ios - 如何在ios中TableviewCell中的uiTextField中实现日期

标签 ios swift datepicker tableview

我可以使用简单的文本字段成功选择日期,但如果是表格 View 单元格中的文本字段,我会遇到问题并且无法选择日期。据我所知,我的操作方法 donePressed 无法正常工作能够在选择日期时在控制台中使用 dateformatter 打印日期。但我无法弄清楚如何在单元格中存在的文本字段中打印该日期。

 let datePicker = UIDatePicker()
    override func viewDidLoad() {
        super.viewDidLoad()
       tableView.reloadData()
        donePressed()
        //createDatePicker()
        // Do any additional setup after loading the view.
    }
    //Private function
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
        cell.dateText.inputAccessoryView = toolBar
        cell.dateText.inputView = datePicker
        cell.dateText.text = "\(datePicker.date)"
        toolBar.setItems([doneBtn], animated: true)
        return cell
    }
    @objc func donePressed() {
        let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        cell1.dateText.text = "\(datePicker.date)"
        self.view.endEditing(true)
    }
   
   /* func createDatePicker()
    {
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
        dateText.inputAccessoryView = toolBar
        dateText.inputView = datePicker
        toolBar.setItems([doneBtn], animated: true)
    }*/

最佳答案

像这样更新选择器中的完成功能。

    @objc func donePressed() {
        let cell1 = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TableViewCell
        cell1.dateText.text = "\(datePicker.date)"
        

self.view.endEditing(true)
}
您需要对单元格的引用,而出队不是为了获取对单元格的引用,而是将单元格出列以显示在表格 View 中。

关于ios - 如何在ios中TableviewCell中的uiTextField中实现日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63156423/

相关文章:

C# Montouch/Xamarin - 相机快门速度

ios - 在构建我的 Cocoapod 时,有没有办法指定对另一个分支的依赖?

knockout.js - Twitter 引导日期选择器在条件 knockout 声明中出现问题

ios - 在继承中我们可以通过 viewDidLoad

ios - 如何在 swift 3.0 中用 * 替换@之前的电子邮件 ID 文本

ios - 保存到服务器后 iOS 图像方向错误的 AS3 air 应用程序

c# - 使用 C# wpf 更改周末日期

memory-management - 如何在 Swift 中实现与 strongify 相同的行为?

arrays - 获取 Swift 集合或数组中对象的下一个或上一个项目

javascript - 3个月到2个日期选择器的范围