gpt4 book ai didi

ios - 注册大量 UICollectionViewCell 类以在 UICollectionView 中重用

转载 作者:行者123 更新时间:2023-11-30 11:35:59 29 4
gpt4 key购买 nike

我正在关注 Advanced User Interfaces with Collection Views WWDC session 上,Apple 工程师展示了一种将多个 UICollectionViewDataSource 对象组合在一起的方法,以实现更好的代码可重用性和关注点分离。

在我的应用程序中,我使用 UICollectionView 并希望实现类似的方法来构建 UI。

UICollectionView 需要在实际使用之前注册类以供重用。

如果我注册应用程序中使用的所有可能的 UICollectionViewCell 子类,而特定屏幕实际上只需要少数子类,是否会出现任何潜在的陷阱?

通过以这种方式设计应用程序,我将无需引入自定义协议(protocol)并查询 DataSource 以了解 UICollectionView 中实际使用的单元格子类。

另一种方法是每次在将单元出队之前注册该单元:

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let model =... GET MODEL
let cellClass = MODEL.cellClass
// Registering the cell class every time before dequeuing, ensuring it will be registered before dequeued
collectionView.registerCellClass(cellClass)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellClass.reuseIdentifier(),
for: indexPath)
cell.configure(model)

return cell
}

这种方法有哪些缺点(如果有的话)?

最佳答案

在出列之前进行注册可能效率非常低,因此我不建议您这样做。

虽然我没有真正的诊断来显示它,但在 viewDidLoad 中注册每个可能的单元类不会对性能产生太大影响,因为这是 Apple 的建议。

您可以根据数据源的类创建单独的方法来注册特定的单元格类。然而,从长远来看,如果您想提高性能或速度,这不是您应该关注的事情。

关于ios - 注册大量 UICollectionViewCell 类以在 UICollectionView 中重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773417/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com