简体   繁体   English

检索UITableView中的单元格

[英]Retrieving cells in UITableView

I have a UITableView that once a cell is clicked, it pushes tableViewB, which contains customcells. 我有一个UITableView,一旦单击一个单元格,它将推送包含自定义单元格的tableViewB。 These cells contain TextFields. 这些单元格包含TextFields。 Once the user does any updating, they click "Save", which then pops tableViewB and goes to the first UITableView. 用户进行任何更新后,他们单击“保存”,然后弹出tableViewB并转到第一个UITableView。 I would like to get all of the UITextField values from tableViewB when Save is clicked. 单击“保存”时,我想从tableViewB获取所有UITextField值。 What is the best way to go about doing that? 这样做的最佳方法是什么?

The problem is that I need to loop through all UITableView cells. 问题是我需要遍历所有UITableView单元格。 I'm not sure how that is done or if it is even a good approach. 我不确定这是怎么做的,或者甚至不是一个好的方法。 Just looking for help on what is a good technique here. 只是在这里寻求帮助,这是什么好技术。

You should be aware that, in general, there are only about as many cell allocated as displayed on the screen. 您应该注意,通常分配的单元格数量与屏幕上显示的数量差不多。 The cells that are not visible are actually not persistent but only get created when tableView:cellForRowAtIndexPath: is called. 不可见的单元实际上不是持久性单元,而是仅在调用tableView:cellForRowAtIndexPath:时创建。 I suggest you create an array to cache the contents of all the text fields and which gets updated whenever a user leaves a text field (eg the textField:shouldEndEditing: method or something like that) is called. 我建议您创建一个数组以缓存所有文本字段的内容,并在用户离开文本字段(例如,textField:shouldEndEditing:方法或类似的东西)时更新该数组。

in your tableViewB header, declare: 在tableViewB标头中,声明:

NSMutableArray *stringArray;

and in the implementation: 并在实施中:

- (id) init {  //whatever your tableViewB initializer looks like
    if ([self = [super init]) {
        //oldData is an NSArray containing the initial values for each text field in order
        stringArray = [[NSMutableArray alloc] initWithArray:oldData];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    //Making the cell
    [cell.textfield addTarget:self action:@selector(updateField:) forControlEvents:UIControlEventValueChanged];
    ....

    //Setting up the cell
    cell.textfield.tag = indexPath.row;
    cell.textfield.text = [stringArray objectAtIndex:indexPath.row];
    return cell;
}

- (void) updateField:(UITextField *)source {
    NSString *text = source.text;
    [stringArray replaceObjectAtIndex:source.tag withObject:text];
}

- (void) dealloc {
    [stringArray release];
}

There are several ways you can choose to get your data back to the original table view, either by delegate, or by having the stringArray declared as a variable passed in to the tableViewB initializer rather than allocated there. 您可以通过几种方式选择将数据返回到原始表视图的方式,既可以通过委托,也可以通过将stringArray声明为变量来传递到tableViewB初始化程序中,而不是在其中进行分配。

如果我理解您的问题-对每个单元格进行数字编号,然后在数组中引用它们/爬升数组以遍历它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM