Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在UITableView中检索单元格_Objective C_Iphone_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c 在UITableView中检索单元格

Objective c 在UITableView中检索单元格,objective-c,iphone,cocoa-touch,uitableview,Objective C,Iphone,Cocoa Touch,Uitableview,我有一个UITableView,一旦单击单元格,它就会推送包含customcells的tableViewB。这些单元格包含文本字段。用户进行任何更新后,单击保存,然后弹出tableViewB并转到第一个UITableView。我想在单击保存时从tableViewB获取所有UITextField值。做这件事最好的方法是什么 问题是我需要遍历所有UITableView单元格。我不确定这是怎么做到的,或者这是否是一个好方法。只是想在这里寻求关于什么是好技术的帮助。如果我理解你的问题-以数字标识每个单元

我有一个UITableView,一旦单击单元格,它就会推送包含customcells的tableViewB。这些单元格包含文本字段。用户进行任何更新后,单击保存,然后弹出tableViewB并转到第一个UITableView。我想在单击保存时从tableViewB获取所有UITextField值。做这件事最好的方法是什么


问题是我需要遍历所有UITableView单元格。我不确定这是怎么做到的,或者这是否是一个好方法。只是想在这里寻求关于什么是好技术的帮助。

如果我理解你的问题-以数字标识每个单元格并在数组中引用它们/爬升数组以循环遍历它们

如果我理解你的问题-以数字标识每个单元格并在数组中引用它们/爬升数组以循环遍历它们

你应该知道,通常,分配的单元格数量与屏幕上显示的单元格数量大致相同。不可见的单元格实际上不是持久的,而是只有在调用tableView:cellForRowAtIndexPath:时才会创建。我建议您创建一个数组来缓存所有文本字段的内容,并在用户离开文本字段时更新该数组,例如,调用textField:shouldEndEditing:method或类似的方法。

您应该知道,一般来说,分配的单元格数量与屏幕上显示的单元格数量差不多。不可见的单元格实际上不是持久的,而是只有在调用tableView:cellForRowAtIndexPath:时才会创建。我建议您创建一个数组来缓存所有文本字段的内容,并在用户离开文本字段时更新该数组,例如调用textField:shouldEndEditing:method或类似的方法。

在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初始值设定项而不是分配到该初始值设定项。

在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初始值设定项而不是在那里分配。

+1回答您的问题。非常有趣。你有没有找到其他的好方法使之成为可能?谢谢。对于其他的好技巧,我的意思是不同于回复中列出的技巧,也很好。你的问题+1。非常有趣。你有没有找到其他的好方法使之成为可能?谢谢。对于其他好的技巧,我的意思是不同于回复中列出的技巧也很好。