将新项添加到UITable视图与reloadData

时间:2013-07-31 09:11:10

标签: ios objective-c

关于UITableView reloadData的文档说(http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html):“不应该在插入或删除行的方法中调用,尤其是在实现的动画块中调用beginUpdates和endUpdates“。

我的问题是,为什么? (特别是第一部分,斜体)。

我正在阅读一本书,它实现了向UITableView添加项目,如下所示:

// Add new item to the table
- (IBAction)addNewItem:(id)sender
{
    // Update the model, add a new item
    BNRItem *newItem = [[BNRItemStore sharedStore] createItem];

    // Figure out where that item is in the array
    int lastRow = [[[BNRItemStore sharedStore] allItems] indexOfObject:newItem];

    // Create the corresponding index path
    NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];

    // Insert this new row into the table
    [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip]
                            withRowAnimation:UITableViewRowAnimationTop];
}

虽然也可以这样做:

// Add new item to the table
- (IBAction)addNewItem:(id)sender
{
    // Update the model, add a new item
    [[BNRItemStore sharedStore] createItem];

    [[self tableView] reloadData];
}

1 个答案:

答案 0 :(得分:1)

通过重新加载数据,您将强制重新创建所有行,因此当您获得有关新信息的信息时,告诉表视图需要添加/删除哪些内容会更加高效,尤其是当您添加一个时最后一行和该部分当前不在屏幕上(因此不必重新渲染视图,只需重新计算滚动高)。