1

I'm following a Advanced User Interfaces with Collection Views WWDC Session, where Apple engineers shown an approach to compose multiple UICollectionViewDataSource objects together to achieve better code reusability and separation of concerns.

In my app I use an UICollectionView and would like to implement a similar approach to building a UI.

An UICollectionView requires registering classes for reuse before actually using them.

Are there any potential pitfalls if I register all the possible UICollectionViewCell subclasses used in the app, when only a handful is actually needed for the particular screen?

By designing the app this way I'll avoid the need to introduce a custom protocol and querying the DataSource for what cell subclasses are actually used in the UICollectionView.

Another approach, is to register the cell every time before dequeuing it:

  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
  }

What are the drawbacks of this approach, if any?

4

1 回答 1

1

在出队之前注册可能非常低效,所以我不建议您使用这些选项。

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

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

于 2018-04-18T02:07:41.657 回答