在UITableViewCell和UITableViewController之间传递数据? [英] Passing data between UITableViewCell and UITableViewController?
问题描述
我在xcode 4.6中创建了主细节模板项目,我添加了具有2个文本字段的自定义单元格。我还创建了一个新类,它是UITableViewCell的子类,在这个类中,我为文本字段创建了outlet。当用户键入东西NSMutableArray更新,这工作正常。现在我想知道如何将此数组传回MasterViewController(UITableViewController),以便我可以使用这些数据来显示计算。
I created master details template project in xcode 4.6 and I added custom cell with 2 textfields. I also created new class which is subclass of UITableViewCell and inside this class I created outlets for text fields. When user types something NSMutableArray is updated and this works fine. Now I am wondering how to pass this array back to MasterViewController (UITableViewController) so that I can use this data to show calculations.
我尝试使用教程在UIViewControllers我不断收到错误。
I tried using tutorials for delegates between UIViewControllers but I keep getting errors. Any help is appreciated.
推荐答案
您不应将数据保存在 UITableViewCell $ c $
You shouldn't keep data inside the UITableViewCell
, as it breaks the MVC.
您需要在单元上获取 UITextField
的引用。这是我在登录表单中的操作:
You need to get a reference of the UITextField
on your cell. This is how I do in a login form:
我有一个自定义单元格子类 TextFieldCell
出口叫 textField
,我想让我的 UITableViewController
引用这些 UITextField
s。
I have a custom cell subclass called TextFieldCell
, it has an outlet called textField
, I want that my UITableViewController
have references to these UITextField
s.
首先我打开我的storyboard,将单元格类设置为TextFieldCell,然后将UITextField连接到单元格的textField outlet。比我添加到 tableView:cellForRowAtIndexPath:
First I open my storyboard, set the cell class to TextFieldCell and than connect the UITextField to cell textField outlet. Than I add this to the 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;
(…)
}
现在我可以访问my loginTextField
和我的 passwordTextField
。我在 tableView:cellForRowAtIndexPath:
上这样做,因为这是在创建要添加到表视图的单元格时。
Now I can access the value of my loginTextField
and my passwordTextField
. I do that on the tableView:cellForRowAtIndexPath:
because that's when I'm creating the cell to add to the table view.
这篇关于在UITableViewCell和UITableViewController之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!