ios - 从 TableViewController 获取 CustomCell 上的 UITextField 的委托(delegate)触发器和普通触发器之间有什么区别?

标签 ios swift uitableview swift2

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/

相关文章:

iOS 应用测试 : No Code signature found

ios - UILabel 看到最右边的内容

ios - Cordova |从 iOS 上的麦克风获取直播

swift - 字符串传输

ios - Swift 中的隐式解包选项似乎不起作用

ios - tableView cellForRowAtIndexPath 中的标签实时文本更新

ios - JSON 文件中的 TableView - 对数据进行排序

ios - 解析 eBay API 时使用未声明的标识符 'cell'

ios - 如何从 Firebase 加载引用数组?

ios - 如何动态扩展 UITableViewCell 高度?