1

I am able to select date succefully using simple textfield ,but in case of textfield within tableview cell,i am facing problem and not able to select date.As far as i am able to identfy my action method donePressed is not working properly as i am able to print date using dateformatter in console on selecting date.but i am unable to figure out how to print that date inside the textfield present in the cell.

 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)
    }*/
  • Don't call `dequeueReusableCell()` outside `tableView(_ tableView:cellForRowAt:)`, and it's unclear what's your issue exactly? Is the date picker not showing? Who's the delegate ? The `donePresse()` method not called? Can't retrieve the date`? – Larme Jul 29 '20 at 15:02
  • sorry for the inconveniences as it is my first question on stackoverflow.Basically datepicker is showing correctly but i am not able to enter selected date inside the textfield,because donepressed method is not getting called .everything else is working fine @Larme – satyam kumar Jul 29 '20 at 15:04
  • Edit your question explaining more what's working and what is not working. This should increase the intervention of users (as your problem is clearer and more specific) and also avoid user to read comments to find out or ask questions in comment. – Larme Jul 29 '20 at 15:05

1 Answers1

1

update the done function in picker as this.

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

self.view.endEditing(true)
}

you need a reference to cell and dequeue is not for get a reference to cell is dequeuing a cell to show in tableview.

Yoel Jimenez del valle
  • 1,270
  • 1
  • 9
  • 20