3

我有一个通过 JSON 加载的 UITableView。所以我的逻辑是这样的:

  1. 获取 JSON(NSOperation,回调主线程)
  2. 在回调中,解析 json,并将新数据插入到我的表的数据源数组中。
  3. 在 TableView 上调用 reloadData 以显示新数据。

我的问题是:我怎样才能动画表中新行的到来?现在我只是让它们同时出现,我想做的是让它们动画到位,因为这看起来更酷。

我想我想要的是使用:

 [self.theTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];

但这让我在运行时抱怨更新的大小与数据源中的大小不同。

我是否遗漏了有关如何使用该方法的信息?有谁知道有人通过这种模式将新行动画到 TableView 中的例子?

更新:基本上,由于 Ben 的指导,我通过以下方法解决了这个问题:

  1. 从 [self.theTableView beginUpdates] 开始;
  2. 当我在 JSON 中循环我的条目时,将它们插入到我的数组的开头,每次循环都会增加它们的索引。
  3. 每次通过循环调用 insertRowsAtIndexPath,如下所示:

    NSIndexPath *indexPath; indexPath = [NSIndexPath indexPathForRow:[self.theChatEntries indexOfObject:message] inSection:0]; [self.theTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];

  4. 以循环外的 endUpdates 结束。

呜呜。

4

1 回答 1

0

你在正确的轨道上;您需要使用 [self.theTableView beginUpdates] 和 [self.theTableView endUpdates] 包装对 -insertRowsAtIndexPaths:withRowAnimation: 的调用。

于 2010-02-08T20:16:22.743 回答