iphone - 在 UITableViewCell 和 UITableViewController 之间传递数据?

标签 iphone ios objective-c xcode cocoa-touch

我在 xcode 4.6 中创建了主细节模板项目,并添加了带有 2 个文本字段的自定义单元格。我还创建了新类,它是 UITableViewCell 的子类,在这个类中我为文本字段创建了导出。当用户键入某些内容时,NSMutableArray 会更新并且可以正常工作。现在我想知道如何将此数组传递回 MasterViewController (UITableViewController),以便我可以使用此数据来显示计算结果。

我尝试对 UIViewController 之间的委托(delegate)使用教程,但我不断收到错误。感谢您的帮助。

最佳答案

您不应该将数据保存在 UITableViewCell 中,因为它会破坏 MVC。

您需要获取单元格上 UITextField 的引用。这是我在登录表单中的做法:

我有一个名为 TextFieldCell 的自定义单元格子类,它有一个名为 textField 的导出,我希望我的 UITableViewController 引用这些 UITextFields.

首先,我打开 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;
    (…)
}

现在我可以访问我的 loginTextFieldpasswordTextField 的值。我在 tableView:cellForRowAtIndexPath: 上执行此操作,因为那时我正在创建要添加到 TableView 的单元格。

关于iphone - 在 UITableViewCell 和 UITableViewController 之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751229/

相关文章:

iphone - MKAnnotation,简单示例

ios - 即使使用 nsthread 接口(interface)也会卡住

iphone - UIView层的内阴影效果?

ios - 我想要一个全局函数,它调用我整个应用程序中的每个按钮单击。这应该是什么小技巧?

ios - 当调用 didFinishNavigation 时,WKWebView 没有完成加载 - WKWebView 中的 Bug?

iphone - 在Objective c中将NSData转换为NSString

ios - 用动画在 UITableview 中一一显示行/单元格

objective-c - 在添加到数组之前格式化为小数点后2位

ios - SWRevealViewController panGestureRecognizer 与 pageViewController iOS(滑动侧菜单)

ios - 将 protected Objective-C 实例变量公开给子类