0

我遇到了这个问题,我在整个互联网上搜索过,但仍然找不到解决方案。请看下面的代码:

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.

问题是,如果我删除滚动和重新加载数据行,程序将正常工作(当然,看不到添加单元格动画)。任何帮助将不胜感激!

4

2 回答 2

0

尝试

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
于 2013-10-21T04:32:49.113 回答
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 

  }

希望你有一些想法:)


于 2013-10-21T05:15:58.517 回答