为 UITableViewcell 上的文本字段设置委托

set delegate for textfields on UITableViewcell

我在 swift 代码中使用 "tags" 来识别 UITableView 内的文本字段。我需要实现 UITextFieldDelegate 函数,如 textfieldshouldreturn 和 textFieldShouldBeginEditing。但在大多数示例中,这些委托函数在 UIView 上显示文本字段,而不是在 UITableView 中。我应该在哪里设置文本字段的委托以及如何设置? (我知道对于UIView下的static textfield,我们在ViewDidiLoad或类似生命周期方法的函数下设置delegate。)

将您的 table 视图作为 IBOutlet 或 属性 的控制器应符合 UITextFieldDelegate,并在 table 视图的数据源方法 cellForRowAt indexPath[=11] 中将文本字段的委托设置为自身=]

yourCell.yourTextField.delegate = self

yourCell.yourTextField.tag = indexPath.row