【发布时间】:2014-06-17 19:41:08
【问题描述】:
我有一个 ViewController,它创建一个新对象,然后返回 FirstViewController.m 我有一个 unwind 方法,它应该用新对象的一行更新 tableView。使用 reloadData 方法将不起作用,因为它会重新加载所有影响我在这些单元格中的方法的单元格。我想只添加一个新单元格而不重新加载整个 tableView。我的 unwind 方法如下所示:
-(IBAction)unwind:(UIStoryboardSegue *)segue {
ABCAddItemViewController *source = [segue sourceViewController];
ABCItem *item = source.object;
if (item != nil) {
[self.objectArray addObject:item];
[self.tableView reloadData];
}
}
我尝试添加这些行而不是 [self.tableView reloadData]:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.objectArray indexOfObject:item] inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
但是,应用程序崩溃并且我收到以下消息:
无效更新:第 0 节中的行数无效。 更新 (1) 后包含在现有节中的行必须是 等于该节之前包含的行数 update (1),加上或减去插入或删除的行数 该部分(插入 1 个,删除 0 个)并加上或减去数量 移入或移出该部分的行(0 移入,0 移出)。
tableView 的行数由[self.objectArray count] 中的the numberOfRowsInSection: 方法决定。
我怎样才能做到这一点?还是有更好的办法?
【问题讨论】:
-
您必须在调用
insertRowsAtIndexPath之前使用附加数据更新您的数据源 (self.objectArray)。 -
我试图只替换
[self.tableView reloadData]行,但这会产生我提到的错误。我在此之前将对象添加到数组中。 -
调用
reloadData不会给出您发布的错误。那只会来自对insertRowsAtIndexPaths:的调用。顺便说一句 - 您需要将该行插入到您将其添加到数组中的同一位置。 -
我不能使用
reloadData,因为重新加载整个表会重新启动一些方法
标签: ios objective-c uitableview reloaddata nsindexpath