我使用它来向我的UITableView添加动画:
[bookMarksArray insertObject:infoDict atIndex:0];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[bookMarksArray count] inSection:0];
[bookMarkTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
当我致电[bookMarkTable reloadData]
时,一切正常。但是,如果我尝试使用以前的代码我的应用程序崩溃错误:尝试将第1行插入第0部分,但更新后第0部分只有1行。有什么想法吗?
答案 0 :(得分:1)
@ Jonathan对异常的解释是正确的。但解决方案仍然是错误的。您已在索引0处插入了一个对象,因此插入行的索引路径将为:
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
如果您已在数组的末尾插入对象,则可以执行
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[bookMarksArray count] - 1 inSection:0];
答案 1 :(得分:0)
创建索引路径(索引路径为0)时需要[bookMarksArray count] - 1
。
答案 2 :(得分:0)
尝试在调用insertRowsAtIndexPath之前更新numberOfRows。