UITableViewCell 上有两个 UITextField 实例。 我想在 CustomCell 上获取 UITextField 的文本。 该函数是 test()。
模式 A 是 UITextField 的代表, 模式B是压力单元功能。
我测试了模式 B 有效,但模式 A 无效。 我其实想让A工作, 这是我的问题, 为什么模式 A 不起作用?
而且,如何从 TableViewController 获取 CustomCell 上的 UITextField 文本?
//Only needed code is written.
protocol CustomTableViewCellDelegate{
func getInputed(textField: UITextField)
}
class CustomTableViewCell: UITableViewCell , UITextFieldDelegate{
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if myTextField != nil {
myTextField.delegate = self
myTextField2.delegate = self
}
}
//the session is to end
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
if delegate != nil {
self.delegate?.getInputed(textField)
}
return true
}
}
class TableViewController: UITableViewController, CustomTableViewCellDelegate{
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Im sure I write like this
cell.delegate = self
}
//Pattern A
func getInputed(textField: UITextField){
test()
}
//Pattern B
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
test()
}
func test(){
self.myTableView.reloadData()
let cell = myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: NSIndexPath(forRow: 0, inSection: 0)) as! CustomTableViewCell
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
}
}
最佳答案
根据我们在评论/聊天中的讨论,我添加了答案。 按以下方式修改代码:
protocol CustomTableViewCellDelegate{
func getInputed(cell:CustomTableViewCell)
}
//the session is to end
func textFieldDidEndEditing(cell:CustomTableViewCell) {
if delegate != nil {
self.delegate?.getInputed(self)
}
}
func test(cell:CustomTableViewCell) {
print(cell.myTextField.text)
print(cell.myTextField.tag)
print(cell.myTextField2.text)
print(cell.myTextField2.tag)
self.myTableView.reloadData()
}
关于ios - 从 TableViewController 获取 CustomCell 上的 UITextField 的委托(delegate)触发器和普通触发器之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36212524/