您不应该将数据保留在 中UITableViewCell,因为它会破坏 MVC。
您需要在您的单元格上获得参考UITextField。这就是我在登录表单中的做法:
我有一个名为 的自定义单元子类TextFieldCell,它有一个名为 的插座textField,我希望我UITableViewController有对这些UITextFields 的引用。
首先,我打开我的故事板,将单元格类设置为 TextFieldCell,然后将 UITextField 连接到单元格 textField 出口。比我把它加到tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
(…)
if (indexPath.row == 0) {
// Sets the textField of the first cell as the loginTextField.
self.loginTextField = tCell.textField;
} else {
// Sets the textField of the second cell as the passwordTextField.
self.passwordTextField = tCell.textField;
}
tCell.textField.delegate = self;
(…)
}
现在我可以访问 myloginTextField和 my的值passwordTextField。我这样做是tableView:cellForRowAtIndexPath:因为那是我创建要添加到表格视图的单元格的时候。