简体   繁体   English

如何显示数据从UIDatePicker到UITableView?

[英]How to Display data from a UIDatePicker into a UITableView?

I would like to get the date from a UIDatePicker and add it to a UITableView cell. 我想从UIDatePicker获取日期并将其添加到UITableView单元格。 在此处输入图片说明

I have tried to put a label into a Table View Cell and update each time the person using the app clicks add date. 我尝试将标签放入表格视图单元格,并在每次使用该应用的用户点击添加日期时进行更新。 However, it is the un-logical way to do it, and Xcode does not let me do it by giving me errors. 但是,这是不合逻辑的方式,Xcode不允许我给我错误。

I have therefore put a label outside the tableview to just test and whenever I click on the Add Date button, the date gets displayed. 因此,我在表视图之外放置了一个标签以进行测试,每当我单击“添加日期”按钮时,就会显示日期。

Here is my code from the view controller folder 这是我的视图控制器文件夹中的代码

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var examDatePicker: UIDatePicker!

@IBOutlet weak var AddClassButton: UIButton!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var datesTable: UITableViewCell!

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    AddClassButton.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func pressButton(button: UIButton) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy"
    let selectedDate = dateFormatter.string(from: examDatePicker.date)


    dateLabel.text = selectedDate

  //  datesTable

    //myDatePicker.datePickerMode = UIDatePickerMode.Date
        }



  }

You need to implement data source method of table view and after select date from datepicker you need to reload tableview and assign that selected date to cell's label 您需要实现表格视图的数据源方法,并且在从日期选择器中选择日期之后,您需要重新加载表格视图并将该选定日期分配给单元格的标签

class ViewController: UIViewController {

 var selectedDate : String ? //Make global variable 

 func pressButton(button: UIButton) {
     let dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "dd MMM yyyy"
     selectedDate = dateFormatter.string(from: examDatePicker.date)
     self.IBtableView.reload()
 }


 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
      return 1 //anything you want
 }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

   let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? YourCustomCell // Or UITableViewCell
   cell. dateLabel.text = selectedDate ?? "SelectDateFromDatePicker"
  }
}

This answer varies slightly from the one provided by Sunil Prajapati, demonstrating using an array to hold the strings, which is I think is a more standard pattern. 这个答案与Sunil Prajapati提供的答案略有不同,表明使用数组来容纳字符串,这是我认为是更标准的模式。

import UIKit

class ViewController: UIViewController {

    // MARK: - Outlets
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var datePicker: UIDatePicker!

    // MARK: - Properties
    var tableSource = [String]()

    // MARK: - Actions
    @IBAction func pickDateButtonTouched(_ sender: Any) {
        // a bit contrived, but you get the idea
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .none
        // get the date
        let dateString = formatter.string(from: datePicker.date)
        // put the string in the data source for the table
        tableSource.insert(dateString, at: 0)
        table.reloadData()
        // profit? 
    }


    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        table.dataSource = self
    }
}

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = tableSource[indexPath.row]
        return cell
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM