ios 如何在 TableviewCell 内的 uiTextField 中实现日期
How to implement date in uiTextField present inside TableviewCell in ios
我能够 select 使用简单的文本字段成功约会,但是如果文本字段在 tableview 单元格中,我面临问题并且无法 select date.As 就我而言我能够识别我的操作方法 donePressed 无法正常工作,因为我能够在 selecting date.but 上使用控制台中的 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)
}*/
将选择器中的 done 函数更新为这样。
@objc func donePressed() {
let cell1 = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TableViewCell
cell1.dateText.text = "\(datePicker.date)"
self.view.endEditing(true)
}
您需要对单元格的引用,出队不是为了获取对单元格的引用,而是使单元格出队以显示在表格视图中。
我能够 select 使用简单的文本字段成功约会,但是如果文本字段在 tableview 单元格中,我面临问题并且无法 select date.As 就我而言我能够识别我的操作方法 donePressed 无法正常工作,因为我能够在 selecting date.but 上使用控制台中的 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)
}*/
将选择器中的 done 函数更新为这样。
@objc func donePressed() {
let cell1 = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! TableViewCell
cell1.dateText.text = "\(datePicker.date)"
self.view.endEditing(true)
}
您需要对单元格的引用,出队不是为了获取对单元格的引用,而是使单元格出队以显示在表格视图中。