iphone - 在 UITableView 中对大量行/节进行动画处理性能不佳

标签 iphone ios uitableview animation

我们不是在谈论数千行或任何东西,尽管如果有办法让事情扩展到那么高,我会很乐意的。

我有一个包含 27 个部分和 180 行的表格,分布在所有部分中,而我目前陷入的场景是当我将事物动画化为只有 3 个部分和 5 行的模型状态时,并且(甚至更糟)又回来了。

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

我尝试过为每一行的删除/添加设置动画,保留部分(在删除情况下将它们的行数降为 0),还尝试为部分本身的删除/插入设置动画(当行数将下降到 0)。我本以为后者会提供更好的性能,但它根本没有改变。

有什么可以在应用程序端完成以加快速度吗?现在我有相当多的代码来摆脱单个动画,如果它们超过 20 个,则选择只重新加载数据。

编辑 这里是展示问题的代码。这段代码的性能略好于等效的单点触摸代码(这是我之前使用的代码),但仍然很糟糕。

#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

最佳答案

只有对可见的行进行动画处理才有意义。不要为要插入的所有行制作动画,而应考虑只为插入那些可见的行制作动画。

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

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

关于iphone - 在 UITableView 中对大量行/节进行动画处理性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474739/

相关文章:

iphone - NSTimeInterval -> 不兼容的指针类型

ios - 如何保存到RestKit中的Core Data RKMappingResult对象?

ios - Google Maps 为点击和未选择状态绘制不同的标记图像

ios - 按下 UITableView "cell"时在文档中加载音频

ios - Xcode 6 为 iPhone 6 和 6 plus 自动调整应用程序大小

显示特定位置的 iPhone 罗盘

iphone - iOS:UITableViewCell 类中的按钮调用主 UIViewController 上的方法

iphone - 手动调用 viewDidAppear 的正确时间?

iphone - 如何设置最小时间每次都显示为UIDatepicker中的默认时间

ios - 图像上的 OCR - iOS