ios - 有没有办法预加载 UICollectionViewCell?

标签 ios uicollectionview uicollectionviewcell

困扰我很久的事情。

有没有办法预加载 UICollectionViewCell ? 在加载数据之前需要准备一些东西,因此在用户滚动时不会创建单元格。

有没有办法完全控制何时创建 UICollectionViewCell 和何时销毁 UICollectionViewCell。

最佳答案

来自 Apple documentation :

You typically do not create instances of this class yourself. Instead, you register your specific cell subclass (or a nib file containing a configured instance of your class) with the collection view object. When you want a new instance of your cell class, call the dequeueReusableCell(withReuseIdentifier:for:) method of the collection view object to retrieve one.

cell 本身不是一个沉重的资源,所以定制它的生命周期是有一点意义的。 我认为与其寻找一种方法来控制单元格的创建,不如问问自己:为什么要预加载单元格?你想预加载什么样的重资源? 根据答案,您可以尝试以下优化:

  • 如果您的单元格中有复杂的 View 层次结构,请考虑从自动布局重构为手动设置框架
  • 如果您的单元格应该显示一些复杂计算的结果或远程图像,您希望有一个单独的架构层来加载这些资源,并且无论如何都不应该在单元格类中进行。必要时使用缓存。

关于ios - 有没有办法预加载 UICollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39687205/

相关文章:

ios - dyld 未找到 AVAssetDownladTask

ios - 或者逻辑运算符在 [NSPredicate predicateWithFormat :] 中不起作用

iOS 媒体库 : React to Access Apple Music Alert

ios - CryptoSwift AES128-CBC 与 Swift

ios - 如何修复 UICollectionViewCell 子类的不兼容指针

ios - CollectionView NSURL session 速度和重新加载

ios - 问 : UITests - How to get collectionCell element inside collectionView nested with TableView cell?

objective-c - 通过切换布局触发动画 UICollectionView 单元格调整大小

ios - 在我使用 forin collectionView.visibleCells 将 cell.selected 设置为 NO 后,选择其他它们再次自动选择

ios - 获取 UIScrollView 内的 UICollectionView 高度