【问题标题】:UITableView Animation HeadacheUITableView 动画头痛
【发布时间】:2010-11-08 23:29:00
【问题描述】:
NSIndexPath* updatedPath = [NSIndexPath indexPathForRow: 0 inSection: 0]; 
NSIndexPath* updatedPath2 = [NSIndexPath indexPathForRow: 1 inSection: 0]; 
NSArray* updatedPaths = [NSArray arrayWithObjects:updatedPath, updatedPath2, nil]; 
[self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop];

上面的代码可以工作并且可以动画。问题是我有很多数据,我不想对行索引进行硬编码。由于我的表部分中只有一个部分可以为 0。如何动态生成 NSIndexPath 对象的 NSArray?

或者有没有更简单的方法来为表格视图设置动画。当用户单击表格视图顶部的选项卡时,表格视图中的所有行都会更改。

【问题讨论】:

    标签: objective-c iphone uitableview view nsarray


    【解决方案1】:

    只是将我的解决方案添加到混合中,因为一旦我找到它就很简单。发生的事情是我有一个表,它是由我自己的代码使用相同的技术“分组”的:cocoanetics.com

    当用户执行需要更新单元格的操作,并且当时没有对表格进行分组时,我的代码正在尝试使用

    [[self tableView] reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];
    

    指定单元格和不存在的标题的路径。这导致了其他人在上面报告的相同错误。我所要做的就是确保提供给 reloadRowsAtIndexPaths:withRowAnimation: 的“路径”确实存在。完成后,一切都很好。

    【讨论】:

      【解决方案2】:

      假设self.arTableData 是一个可变的表数组,tableViewUITableView 的一个实例。假设tableView 中有 10 行(这意味着数组中有 10 个对象)。 我确实实现了以下代码来删除动画行。

      int i; // to remove from 3 to 6
      for(i=2;i<6;i++)
      {
          [self.arTableData removeObjectAtIndex:i];
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
      }
      

      这个技巧对我很有效,没有任何麻烦。希望它对你也一样。祝你好运。

      【讨论】:

        【解决方案3】:

        如果要刷新整个部分:

        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
        

        【讨论】:

          【解决方案4】:

          我终于让它工作了。代码如下:

          NSMutableArray *indexPaths = [[[NSMutableArray alloc] init] autorelease];
          for (int i = 0; i < [mySexyList count]; i++) {
             NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:i inSection:0];
             [indexPaths addObject:updatedPath];
          }
          
          [self.myFunkyTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
          

          问题是我从 numberOfRowsInSection: 方法返回硬编码值,这与 mySexyList 大小不同。这导致应用程序崩溃。

          【讨论】:

            【解决方案5】:

            如果您有多个具有相同行索引的 indexPathForRow,则会出现此异常。

            用于示例

            [segmentTable reloadRowsAtIndexPaths: [[NSArray alloc] initWithObjects: 
                                            [NSIndexPath indexPathForRow: 1 inSection: 1], 
                                            [NSIndexPath indexPathForRow: 1 inSection: 1], nil]                                 withRowAnimation:UITableViewRowAnimationNone];
            

            【讨论】:

              【解决方案6】:

              要生成索引路径数组,您可以循环:

                  NSMutableArray *updatedPaths = [NSMutableArray array];
                  for (NSNumber *row in someArray) {
                      NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:[row intValue] inSection:0];
                      [updatedPaths addObject:updatedPath];
                  }
                  [self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop];
              

              如果你重新加载 TableView 中的所有数据,为什么不直接调用 reloadData 方法呢?

              【讨论】:

              • 当我运行你的代码时,我得到这个: *** Assertion failure in -[_UITableViewUpdateSupport _setupAnimationsForNewlyInsertedCells], /SourceCache/UIKit/UIKit-963.10/UITableViewSupport.m:541 2009-07-08 09: 14:42.979 QuartzFun[3781:207] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“尝试为单元格创建两个动画”2009-07-08 09:14:42.979 QuartzFun[3781:207] 堆栈: (
              • 如果我循环:NSMutableArray updatedPaths = [[NSMutableArray alloc] init]; for (int i = 0; i -[NSMutableIndexSet addIndexesInRange:]: Range {214747, 1} 超过 NSNotFound 的最大索引值 - 1' QuartzFun[3810 :207]
              • 如果这仅适用于 5 行之类的,您可以尝试一下吗?在 for 循环中,增加一个计数器,当它达到 5 时,中断。那它有用吗?
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-07-29
              • 1970-01-01
              • 1970-01-01
              • 2013-05-04
              • 1970-01-01
              • 2014-07-31
              • 2012-09-07
              相关资源
              最近更新 更多