[英]Retrieving cells in UITableView
我有一个UITableView,一旦单击一个单元格,它将推送包含自定义单元格的tableViewB。 这些单元格包含TextFields。 用户进行任何更新后,他们单击“保存”,然后弹出tableViewB并转到第一个UITableView。 单击“保存”时,我想从tableViewB获取所有UITextField值。 这样做的最佳方法是什么?
问题是我需要遍历所有UITableView单元格。 我不确定这是怎么做的,或者甚至不是一个好的方法。 只是在这里寻求帮助,这是什么好技术。
您应该注意,通常分配的单元格数量与屏幕上显示的数量差不多。 不可见的单元实际上不是持久性单元,而是仅在调用tableView:cellForRowAtIndexPath:
时创建。 我建议您创建一个数组以缓存所有文本字段的内容,并在用户离开文本字段(例如,textField:shouldEndEditing:方法或类似的东西)时更新该数组。
在tableViewB标头中,声明:
NSMutableArray *stringArray;
并在实施中:
- (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];
}
您可以通过几种方式选择将数据返回到原始表视图的方式,既可以通过委托,也可以通过将stringArray声明为变量来传递到tableViewB初始化程序中,而不是在其中进行分配。
如果我理解您的问题-对每个单元格进行数字编号,然后在数组中引用它们/爬升数组以遍历它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.