如何调用UFableViewCell(swift)内部的textField

时间:2018-05-15 09:30:36

标签: swift uitableview subclass textfield

我有onDoubleClick = (e: any): void => { this.setInputValuesAndSetFocus(e); } onBtnPencilClick = (e: any): void => { this.setInputValuesAndSetFocus(e); } private setInputValuesAndSetFocus = (e: any): void => { this.showButtons = true; this.readField = false; this.onFocused = true; e.focus(); } 内的UITextField

UITableViewCell

如何将相同的textField传递给:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TextFieldTableViewCell
    let textField: UITextField = cell.textField
    let detail = self.detailItem

    textField.text = detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description
    print()
    return cell
}

解决

我需要在定义后添加func textFieldDidBeginEditing(textField: UITextField) { print("executed") let detail = self.detailItem let textField: UITextField = self.text.textField if (textField.text != detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description) { detail?.setValue(textField.text, forKey: "name") } }

我假设它将视图连接到视图控制器,允许它看到textField.delegate = self函数。

1 个答案:

答案 0 :(得分:0)

你需要在cellForRowAtIndexPath方法本身中提供UITextFieldDelegate

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TextFieldTableViewCell
    let textField: UITextField = cell.textField

    textField.delegate = self

    let detail = self.detailItem

    textField.text = detail!.valueForKey("name")!.description + detail!.valueForKey("number")!.description

    return cell

}