预填充 UICollectionView 单元重用队列 [英] Prefilling the UICollectionView cell reuse queue

查看:32
本文介绍了预填充 UICollectionView 单元重用队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我有一个带有单个 UICollectionView 的应用程序,当我第一次滚动它时就卡住了.我将来源缩小到正在创建新单元格 (2) 的事实(使用 initWithFrame:),因为周围没有可重复使用的单元格.在初始滚动之后,重用队列不为空,并且可以重用单元格并且不会出现卡顿现象.

I have an app with a single UICollectionView that's stuttering the first time that I scroll it. I've narrowed the source down to the fact that new cells (2) are being created (using initWithFrame:) because there are no cells lying around to be reused. After that initial scroll, the reuse queue isn't empty and cells can be reused and there's no stuttering.

黑客

因此,我已经能够借助 iOS 运行时标头:

So I've been able to workaround the problem with help of a private method in the iOS runtime headers:

- (void)_reuseCell:(id)arg1;

我的假设是,这是将单元格添加回重用队列的位置.使用这种未记录方法的黑客看起来像这样:

My assumption is that this is where cells are being added back into the reuse queue. The hack to use this undocumented method looks like this:

int prefillCellCount = 10;
NSMutableArray *cells = [[NSMutableArray alloc] initWithCapacity:prefillCellCount];
for (int i=0; i<10prefillCellCount i++) {
    UICollectionViewCell *cell = [self.gridView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    [cells addObject:cell];
}
for (UICollectionViewCell *cell in cells) {
    [self.gridView _reuseCell:cell];
}

黑客成功了!滚动时没有口吃.

The hack works! There's no stuttering when scrolling.

问题

我认为我无法通过此黑客攻击通过 App Store 的评论者.有没有办法在不使用未记录的方法的情况下做到这一点?如果没有,我唯一的手段是混淆黑客吗?

I don't think I'll be able to get past the App Store reviewers with this hack. Is there a way to do this without the use of undocumented methods? If not, is my only resort just to obfuscate the hack?

推荐答案

有没有办法在不使用未记录的方法的情况下做到这一点?

Is there a way to do this without the use of undocumented methods?

如果问题仅仅是创建单元格需要很长时间,那么您可以简单地创建一些额外内容并在您的视图控制器中保留它们,直到集合要求它们.像您在代码中所做的那样预先分配一些单元格,调用 dequeueReusableCellWithReuseIdentifier: 来获取集合来为您创建它们.将这些保存在您的视图控制器中的数组中.接下来,在您的 ...cellForItemAtIndexPath: 方法中,如果数组不为空,则从数组中删除一个单元格并使用它而不是向集合视图询问可重用的单元格.

If the problem is simply that it takes a long time to create the cells, then you can simply create a few extras and hold onto them in your view controller until the collection asks for them. Pre-allocate a few cells as you're doing in your code already, calling dequeueReusableCellWithReuseIdentifier: to get the collection to create them for you. Save these in an array in your view controller. Next, in your ...cellForItemAtIndexPath: method, if the array isn't empty remove a cell from the array and use it instead of asking the collection view for a reusable cell.

这应该通过移动创建单元格的点而不使用任何未记录的方法来解决您的问题.

This should solve your problem by moving the point at which the cells are created without using any undocumented methods.

另一种方法是加快单元格创建过程,这样您就不必费心预先分配单元格.分析您的代码以了解为什么您的单元格需要这么长时间来构建.也许您可以通过删除不必要的视图来简化单元格结构?

An alternative approach is to speed up the cell creation process so that you don't have to bother pre-allocating cells. Profile your code to see why your cells take so long to construct. Maybe you can simplify your cell structure by removing unnecessary views?

这篇关于预填充 UICollectionView 单元重用队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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