带有自定义 UITableViewCell 的 UITableView 中的 UITextField 中的 UIDatePicker:实时更新
UIDatePicker in UITextField in UITableView with custom UITableViewCell: realtime update
我遇到的情况与这个问题完全相同:
UIDatePicker in UITextField in UITableView realtime update
总而言之:我在调用 UIDatePicker 的自定义单元格中有一个文本字段。我希望在修改日期选择器时更新文本字段,并且我希望它在点击 "Done" 按钮(位于为日期选择器创建的工具栏内)时存储最后一个值。
我可以获取在 pickerview 中选择的值(我实际上将它打印到控制台),但我无法将该值放入文本字段中。
但是,我需要在 swift 内完成,但我无法完成。我一直在尝试使用 action: #selector() 但由于它在选择器内部的函数中不接受超过 1 个参数,所以它对我没有帮助。
自定义 Table 查看单元格:
class employeesTableViewCell : UITableViewCell {
@IBOutlet weak var employeeNumberField: UILabel!
@IBOutlet weak var employeeNameField: UITextField!
@IBOutlet weak var employeeSurnameField: UITextField!
@IBOutlet weak var employeeIDField: UITextField!
@IBOutlet weak var employeeBirthDateTxtFieldOutlet: UITextField!
}
在ViewController中(下面的标签策略用于捕获通过textFieldShouldReturn编辑的字段):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "employeeCell") as! employeesTableViewCell!
createPickerView(sender: (cell?.employeeBirthDateTxtFieldOutlet)!)
createToolbar(sender: (cell?.employeeBirthDateTxtFieldOutlet)!)
cell?.employeeNameField.tag = indexPath.row * 10 + 1
cell?.employeeSurnameField.tag = indexPath.row * 10 + 2
cell?.employeeIDDocField.tag = indexPath.row * 10 + 3
cell?.employeeBirthDateTxtFieldOutlet.tag = indexPath.row * 10 + 5
cell?.employeeNumberField.text = "Employee \(indexPath.row + 1)"
return cell!
}
func createPickerView(sender: UITextField){
let datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.tag = sender.tag
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(caller:destination:)), for: UIControlEvents.valueChanged)
}
@objc func datePickerValueChanged(caller: UIDatePicker, destination: UITextField){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
destination.text = dateFormatter.string(from: caller.date) <-- I expected I could update the text here, as I'm referencing it through the parameter, but it doesn't work. However, no error is raised.
}
func createToolbar(sender: UITextField){
let datePickerToolbar = UIToolbar()
datePickerToolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(employeeRegisterController.dismissKeyboard))
doneButton.tag = sender.tag
datePickerToolbar.setItems([doneButton], animated: false)
datePickerToolbar.isUserInteractionEnabled = true
sender.inputAccessoryView = datePickerToolbar
}
@objc func dismissKeyboard(on: UIButton){
view.endEditing(true)
}
您不能在#selector() 中传递一个以上的参数,但您可以使用附加参数传递位置
然后改变下面两个函数
func createPickerView(sender: UITextField){
let datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.tag = sender.tag
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(caller:)), for: UIControlEvents.valueChanged)
}
func datePickerValueChanged(caller: UIDatePicker){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
let indexRow = (caller.tag - 5) / 10
let indexPath = IndexPath(row: indexRow, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! employeesTableViewCell
cell.employeeBirthDateTxtFieldOutlet.text = dateFormatter.string(from: caller.date)
}
希望对您有所帮助
我遇到的情况与这个问题完全相同:
UIDatePicker in UITextField in UITableView realtime update
总而言之:我在调用 UIDatePicker 的自定义单元格中有一个文本字段。我希望在修改日期选择器时更新文本字段,并且我希望它在点击 "Done" 按钮(位于为日期选择器创建的工具栏内)时存储最后一个值。
我可以获取在 pickerview 中选择的值(我实际上将它打印到控制台),但我无法将该值放入文本字段中。
但是,我需要在 swift 内完成,但我无法完成。我一直在尝试使用 action: #selector() 但由于它在选择器内部的函数中不接受超过 1 个参数,所以它对我没有帮助。
自定义 Table 查看单元格:
class employeesTableViewCell : UITableViewCell {
@IBOutlet weak var employeeNumberField: UILabel!
@IBOutlet weak var employeeNameField: UITextField!
@IBOutlet weak var employeeSurnameField: UITextField!
@IBOutlet weak var employeeIDField: UITextField!
@IBOutlet weak var employeeBirthDateTxtFieldOutlet: UITextField!
}
在ViewController中(下面的标签策略用于捕获通过textFieldShouldReturn编辑的字段):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "employeeCell") as! employeesTableViewCell!
createPickerView(sender: (cell?.employeeBirthDateTxtFieldOutlet)!)
createToolbar(sender: (cell?.employeeBirthDateTxtFieldOutlet)!)
cell?.employeeNameField.tag = indexPath.row * 10 + 1
cell?.employeeSurnameField.tag = indexPath.row * 10 + 2
cell?.employeeIDDocField.tag = indexPath.row * 10 + 3
cell?.employeeBirthDateTxtFieldOutlet.tag = indexPath.row * 10 + 5
cell?.employeeNumberField.text = "Employee \(indexPath.row + 1)"
return cell!
}
func createPickerView(sender: UITextField){
let datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.tag = sender.tag
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(caller:destination:)), for: UIControlEvents.valueChanged)
}
@objc func datePickerValueChanged(caller: UIDatePicker, destination: UITextField){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
destination.text = dateFormatter.string(from: caller.date) <-- I expected I could update the text here, as I'm referencing it through the parameter, but it doesn't work. However, no error is raised.
}
func createToolbar(sender: UITextField){
let datePickerToolbar = UIToolbar()
datePickerToolbar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(employeeRegisterController.dismissKeyboard))
doneButton.tag = sender.tag
datePickerToolbar.setItems([doneButton], animated: false)
datePickerToolbar.isUserInteractionEnabled = true
sender.inputAccessoryView = datePickerToolbar
}
@objc func dismissKeyboard(on: UIButton){
view.endEditing(true)
}
您不能在#selector() 中传递一个以上的参数,但您可以使用附加参数传递位置
然后改变下面两个函数
func createPickerView(sender: UITextField){
let datePickerView : UIDatePicker = UIDatePicker()
datePickerView.datePickerMode = UIDatePickerMode.date
sender.inputView = datePickerView
datePickerView.tag = sender.tag
datePickerView.addTarget(self, action: #selector(datePickerValueChanged(caller:)), for: UIControlEvents.valueChanged)
}
func datePickerValueChanged(caller: UIDatePicker){
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
let indexRow = (caller.tag - 5) / 10
let indexPath = IndexPath(row: indexRow, section: 0)
let cell = tableView.cellForRow(at: indexPath) as! employeesTableViewCell
cell.employeeBirthDateTxtFieldOutlet.text = dateFormatter.string(from: caller.date)
}
希望对您有所帮助