【问题标题】:UITableView exception when scroll and add cell滚动和添加单元格时的 UITableView 异常
【发布时间】:2013-10-21 04:28:55
【问题描述】:

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

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.

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

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    试试

    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexOfCreatedCard inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:NO];
    

    【讨论】:

      【解决方案2】:

      我认为问题是,您在插入单元格之前正在滚动,我只是发布适合您问题的示例代码


       - (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 
      
        }
      

      希望你有一些想法:)


      【讨论】:

      • 我确实在滚动之前调用了 [tableView reloadData],应该在 tableView 滚动之前添加单元格。我尝试插入然后滚动,应用程序没有崩溃。虽然看不到插入动画,但这不是我想要实现的。
      • 使用 [aTableView beginUpdates];和 [aTableView endUpdates];用于插入动画并设置 aTableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);调整“contentInset”底部空间中的值以查看动画查看我更新的代码您可能需要更改“contentInset”值
      • 我添加了更改内容插入代码,现在它只滚动到新单元格之前的右侧,删除了这些内容插入代码,它直接滚动到添加的单元格但没有动画。问题是,如果新单元格在屏幕上(内容大小小于高度),则会显示动画。
      • 那就是每次都将 contentInset 设置为一些计算值,这样你就可以获得整个动画
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多