[英]UITableView exception when scroll and add cell
我遇到了这个问题,我已经在整个互联网上进行搜索,但仍然找不到解决方案。 请看下面的代码:
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.
关键是,如果我删除滚动条和重新加载数据行,则程序将正常运行(当然,无法看到添加的单元格动画)。 任何帮助将不胜感激!
尝试
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
我认为问题是,在插入单元格之前您正在滚动,我只是发布了适合您问题的示例代码
- (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
}
希望你有一些主意:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.