UITableViewCell 使用 AutoLayout 时性能不佳 [英] UITableViewCell bad performance with AutoLayout

查看:18
本文介绍了UITableViewCell 使用 AutoLayout 时性能不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点坚持这个......非常感谢任何帮助.我已经花了很多时间调试这个.

I'm somewhat stuck with this one… any help is very appreciated. I've already spent lots of time debugging this.

我有 UITableViewNSFetchedResultsController 提供的数据源.在一个单独的视图控制器中,我使用 [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] 向 CoreData 插入新记录,保存托管对象上下文并关闭该控制器.非常标准的东西.

I've got UITableView with data source provided by NSFetchedResultsController. In a separate view controller I insert new records to the CoreData using [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:], save the managed object context and dismiss that controller. Very standard stuff.

然后由 NSFetchedResultsController 接收托管对象上下文中的更改:

The changes in managed object context are then received by NSFetchedResultsController:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationNone];

            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

            break;

        case NSFetchedResultsChangeUpdate:
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

            break;

        case NSFetchedResultsChangeMove:
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationNone];

            break;
    }
}

这就是问题出现的地方——这需要很长时间(在 iPhone 4 上大约需要 3-4 秒)才能完成.似乎时间都花在了计算单元格的布局上.

And this is where the problem appears — it takes too long(about 3-4 seconds on an iPhone 4) to do that. And it seems like the time is spent calculating layout for the cells.

我已经从单元格中剥离了所有内容(包括自定义子类),只留下了 UILabel,但没有任何改变.然后我将单元格的样式更改为基本(或除自定义之外的任何内容),问题就消失了——新的单元格会立即添加.

I've stripped everything from the cell(including custom subclass) and left it with just UILabel, but nothing changed. Then I've changed the style of the cell to Basic(or anything except Custom) and the problem disappeared — new cells are added instantaneously.

我已经加倍检查并且 NSFetchedResultsControllerDelegate 回调只被调用一次.如果我忽略它们并执行 [UITableView reloadSections:withRowAnimation:],没有任何变化——它仍然很慢.

I've doubled checked and NSFetchedResultsControllerDelegate callbacks are called only once. If I ignore them and do [UITableView reloadSections:withRowAnimation:], nothing changes — it is still very slow.

在我看来,默认单元格样式禁用了自动布局,这使得它们非常快.但如果是这样的话——为什么当我推送 UITableViewController 时一切都加载得很快?

It seems to me like Auto Layout is disabled for the default cell styles, which makes them very fast. But if that is the case — why does everything loads quickly when I push the UITableViewController?

这是该问题的调用跟踪:

Here's the call trace for that problem:

所以问题是——这里发生了什么?为什么细胞渲染这么慢?

So the question is — what is going on here? Why are cells being rendered so slowly?

更新 1

我构建了一个非常简单的演示应用程序来说明我遇到的问题.这是来源 - https://github.com/antstorm/UITableViewCellPerformanceProblem

I've built a very simple demo app that illustrates the problem I'm having. here's the source — https://github.com/antstorm/UITableViewCellPerformanceProblem

尝试添加至少一屏单元格来感受性能问题.

Try adding at least a screenful of cells to feel the performance problems.

另请注意,直接添加一行(立即插入!"按钮)不会导致任何缓慢.

Also note that adding a row directly ("Insert now!" button) is not causing any slowness.

推荐答案

好吧,我终于在不牺牲动画的情况下解决了这个问题.我的解决方案是在禁用 AutoLayout 的单独 Nib 文件中实现 UITableViewCell 的界面.加载时间稍长,需要自己定位子视图.

Ok, I finally got around this problem without sacrificing animation. My solution is to implement UITableViewCell's interface in a separate Nib file with AutoLayout disabled. It takes a little bit longer to load and you need to positions subviews yourself.

这是实现它的代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    ...        

    UINib *rowCellNib = [UINib nibWithNibName:@"RowCell" bundle:nil];
    [self.tableView registerNib:rowCellNib forCellReuseIdentifier:@"ROW_CELL"];
}

当然,您需要一个包含单元格视图的 RowCell.nib 文件.

Of course you'll need a RowCell.nib file with your cell's view.

虽然原始问题没有解决方案(这对我来说显然是一个错误),但我正在使用这个.

While there's no solution to the original problem(which clearly seems a bug to me), I'm using this one.

这篇关于UITableViewCell 使用 AutoLayout 时性能不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆