我遇到了这个问题,我在互联网上搜索过,但仍无法找到解决方案。请看下面的代码:
NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 4
[self updateCardIDArray];
NSLog(@"%i", [self tableView:tableView numberOfRowsInSection:0]); // 5
int indexOfCreatedCard = [cardIDs indexOfObject:newCardID];
NSLog(@"%i", indexOfCreatedCard); // 4
[tableView reloadData];
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:FALSE];
NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0], nil];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
基本上,我在这里尝试实现的是将表格滚动到刚创建的单元格,然后显示正在添加的动画。当我使用此代码时,我有这样的错误:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
和这个例外:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.
问题是,如果我删除滚动和重新加载数据行,程序将正常工作(当然,无法看到添加单元格动画)。任何帮助将不胜感激!
答案 0 :(得分:0)
尝试
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
答案 1 :(得分:0)
我认为问题是,你在插入单元格之前滚动,我只是发布适合你问题的示例代码
- (IBAction)scrollPlease:(id)sender
{
aTableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0); //to move some position below
k = k + 1; //hear k is the row numbers in ur case "indexOfCreatedCard"
[aTableView beginUpdates]; // first u add the cell to tableview
[aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:k-1 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
[aTableView endUpdates]; //hear (k - 1) is the last row
[aTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:k-1 inSection:0] atScrollPosition:UITableViewRowAnimationTop animated:NO]; //then scroll to last cell it will show the animation of adding the cell
aTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); // after u set it to zero
}
希望你有所了解:)