我在 xcode 4.6 中创建了主细节模板项目,并添加了带有 2 个文本字段的自定义单元格。我还创建了新类,它是 UITableViewCell 的子类,在这个类中我为文本字段创建了导出。当用户键入某些内容时,NSMutableArray 会更新并且可以正常工作。现在我想知道如何将此数组传递回 MasterViewController (UITableViewController),以便我可以使用此数据来显示计算结果。
我尝试对 UIViewController 之间的委托(delegate)使用教程,但我不断收到错误。感谢您的帮助。
最佳答案
您不应该将数据保存在 UITableViewCell
中,因为它会破坏 MVC。
您需要获取单元格上 UITextField
的引用。这是我在登录表单中的做法:
我有一个名为 TextFieldCell
的自定义单元格子类,它有一个名为 textField
的导出,我希望我的 UITableViewController
引用这些 UITextField
s.
首先,我打开 Storyboard,将单元格类设置为 TextFieldCell,然后将 UITextField 连接到单元格 textField socket 。比我将其添加到 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;
(…)
}
现在我可以访问我的 loginTextField
和 passwordTextField
的值。我在 tableView:cellForRowAtIndexPath:
上执行此操作,因为那时我正在创建要添加到 TableView 的单元格。
关于iphone - 在 UITableViewCell 和 UITableViewController 之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751229/