从 ios 5 开发开始就有一个解决方案算法。
1.在prepareSegue中取indexPath
// prepare selection info
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
2.将其发送到第二个视图控制器
UIViewController *destination = segue.destinationViewController;
[destination setValue:self forKey:@"delegate"];
id object = [self.tasks objectAtIndex:indexPath.row];
NSDictionary *selection = [NSDictionary dictionaryWithObjectsAndKeys:indexPath,@"indexPath",object,@"object", nil];
[destination setValue:selection forKey:@"selection"];
3.第二个视图控制器稍后会将indexPath发回给第一个控制器
// prepare selection info back
NSIndexPath *indexPath = [_selection objectForKey:@"indexPath"];
id object = _textView.text;
NSDictionary *editedSelection = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", object, @"object", nil];
[_delegate setValue:editedSelection forKey:@"editedSelection"];
4.因此在第一个视图控制器的getter方法中,该方法用于从第二个视图控制器取回indexPath,您通过indexPath取回表格单元格并更改其值
#pragma mark - Call back by destination view
-(void)setEditedSelection:(NSDictionary *)dict{
NSIndexPath *indexPath = [dict objectForKey:@"indexPath"];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
....
}