在UITableView中制作大量行/节的动画效果不佳

tos*_*hok 10 iphone animation uitableview ios

我们不会谈论数千行或任何事情,但如果有办法让事情扩大到那么高,我会喜欢它.

我有一个包含27个部分和180行遍布所有部分的表格,而我现在陷入困境的情景是当我将事物设置为只有3个部分和5个行的模型状态时,以及(甚至更糟)再次返回.

我正在使用beginUpdates/endUpdates对所有动画进行批处理.我的应用程序很好地锁定了iphone4上的1-2秒,同时它解决了问题,然后动画开始.

我已经尝试了动画删除/添加每一行,保持周围的部分(并在删除情况下将它们的行数减少到0),并且还动画只是删除/插入部分本身(当行计数时已降至0).我会假设后者会提供更好的表现,但它根本没有改变.

有没有什么可以在应用程序端完成加快这一点?现在,如果有超过20个动画,我会有相当多的代码来摆脱单个动画,而选择仅重新加载数据.

编辑这里显示问题的代码.这段代码的性能略好于等效的monotouch代码(这是我之前使用的),但它仍然非常糟糕.

#import "TableViewController.h"

@interface MyTableViewDataSource : NSObject<UITableViewDataSource> {
    int rows;
};

@end

@implementation MyTableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)setRowCount:(int)r
{
    rows = r;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.textLabel.text = [NSString stringWithFormat:@"row %d", indexPath.row];

    return cell;
}

@end

@implementation MyTableViewController {
    UIBarButtonItem *populateButtonItem;
};

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        populateButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Populate" style:UIBarButtonItemStylePlain target:self action:@selector(populateDataSource)];
    }
    return self;
}

- (void)populateDataSource
{
    NSMutableArray* new_rows = [[NSMutableArray alloc] init];
    [((MyTableViewDataSource*)self.tableView.dataSource) setRowCount:200];

    for (int i = 0; i < 200; i ++)
        [new_rows addObject:[NSIndexPath indexPathForRow:i inSection:0]];

    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:new_rows withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView.dataSource = [[MyTableViewDataSource alloc] init];
    self.navigationItem.rightBarButtonItem = populateButtonItem;
}

@end
Run Code Online (Sandbox Code Playgroud)

Cal*_*leb 3

只有对可见的行进行动画处理才有意义。不要对要插入的所有行执行动画,而应考虑仅对可见行的插入进行动画处理。

另外,您确定是动画导致了延迟吗?如果你通过UITableViewRowAnimationNone动画,你会得到同样的延迟,还是更快?如果速度更快,那么再次避免对那些不可见的插入进行动画处理。(您可以使用 找出哪些行当前可见-indexPathsForVisibleRows。)如果速度不快,那么问题可能根本与动画无关,而是一次插入几百行的开销。像您现在所做的那样重新加载整个表是一种选择;以较小的批次插入行是另一回事。

最后,在执行插入时使用 Instruments 分析您的应用程序是个好主意。您将更好地了解应用程序在延迟期间正在执行的操作,这是消除延迟的第一步。