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

时间:2012-05-06 22:14:31

标签: iphone ios uitableview animation

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

我有一个包含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

4 个答案:

答案 0 :(得分:3)

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

另外,你确定这是造成延迟的动画吗?如果您为动画传递UITableViewRowAnimationNone,或者速度更快,会得到相同的延迟吗?如果它更快,那么再次避免动画那些不可见的插入。 (您可以使用-indexPathsForVisibleRows找出当前可见的行。)如果它不是更快,那么问题可能根本与动画无关,而是一次插入几百行的开销。正如您现在所做的那样重新加载整个表格是一种选择;以较小批量插入行是另一种。

最后,在插入时用仪器配置应用程序是个好主意。您将更好地了解该应用在延迟期间所做的工作,这是消除延迟的第一步。

答案 1 :(得分:1)

您正在使用UITableViewRowAnimationAutomatic(参考:表格视图为您选择合适的动画样式。(在iOS 5.0中引入。)),由于某种原因,表格视图选择了一个非常糟糕的一个,在扩展它们的同时淡化所有行。在调整框架大小并在HAS周围移动时,在200 UIViews上更改不透明度会变慢。 :)

你可以这样做:

[self.tableView insertRowsAtIndexPaths:new_rows withRowAnimation:UITableViewRowAnimationNone];

这会在插入行时将动画减少为UITableView的基本动画,在我看来,插入行时绝对足够。

答案 2 :(得分:1)

你可以尝试让你自定义UITableViewCell和自定义绘图,检查这个项目,te部分讨论TableView和快速单元格。

http://iosboilerplate.com/#uitableview

答案 3 :(得分:0)

尝试使用一些硬编码的字符串数组测试相同的功能,看看问题已经消失。如果问题仍然存在,则意味着问题在于渲染不是数据。

对于不同类型的动画,已经制作了一些自定义控件,您可以通过一些很好的单线程实现获得更快的动画:Here看看。