UICollectionViewCompositionalLayout无法使用与UICollectionViewDiffableDataSource相同的部分?

问题描述

我正在使用(新的)新的可扩散数据源和可组合的collectionview布局构建一个具有集合视图的视图控制器。我创建了自己的Section和Row枚举,可以对模型(一组照片)中的更改做出反应,从而触发applySnapshot,一切正常。

但是...在我的createLayout函数中,我只有一个区段索引可以使用?这对我来说似乎很奇怪,为什么这两种技术不能更好地协同工作?一个适用于SectionIdentifierTypeItemIdentifierType(太棒了!),另一个仍基于索引? gh。

我想要做的是这样的:Section.header部分中的所有单元格都必须是全屏宽度,并且具有自动高度。 Section.photos节中的每个单元格都将具有固定的大小。并且可能还有其他单元格大小不同的部分,并且某些部分是可选的。因此,基于索引的系统确实很糟糕,无法硬编码第0部分是标题,第1部分是照片:顺序可以更改,某些事情可能是可选的,等等。

有什么更好的办法来解决这个问题?我当然可以在视图控制器上存储数组[Section],使用节索引以这种方式获取Section的情况,但是没有内置的方法获取索引的节标识符吗? since由于这两种技术是同时发布的,所以这似乎是合乎逻辑的事情。

class PhotosViewController: UIViewController {
  enum Section: Equatable,Hashable {
    case header
    case photos
  }

  enum Row: Equatable,Hashable {
    case header
    case photo(Photo)
  }

  private lazy var dataSource = makeDataSource()
  var photos: AnyPublisher<[Photo],Never>!

  override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.collectionViewLayout = createLayout()

    photos
      .sink(receiveValue: applySnapshot)
      .store(in: &subscriptions)
  }

  private func makeDataSource() -> UICollectionViewDiffableDataSource<Section,Row> {
    return UICollectionViewDiffableDataSource(collectionView: collectionView) { (collectionView,indexPath,item) -> UICollectionViewCell? in
      switch item {
        case .header:
          let cell = collectionView.dequeueReusableCell(for: indexPath,cellType: ProfileHeaderCollectionViewCell.self)
          return cell

        case .photo(let photo):
          let cell = collectionView.dequeueReusableCell(for: indexPath,cellType: PhotoCollectionViewCell.self)
          cell.configure(photo: photo)
          return cell
      }
    }
  }

  private func applySnapshot(photos: [Photo]) {
    var snapshot = NSDiffableDataSourceSnapshot<Section,Row>()

    let headerSection = Section.header
    snapshot.appendSections([headerSection])
    snapshot.appendItems([.header],toSection: headerSection)

    let items = photos.map { Row.photo($0) }
    let photosSection = Section.photos
    snapshot.appendSections([photosSection])
    snapshot.appendItems(items,toSection: photosSection)

    dataSource.apply(snapshot,animatingDifferences: false)
  }

  private func createLayout() -> UICollectionViewLayout {
    let layout = UICollectionViewCompositionalLayout { [uNowned self] sectionIndex,_ in
      // I have to work with a sectionIndex instead of a Section case?
    }

    return layout
  }
}

解决方法

幸运的是,您不必使用单独的数组来跟踪节标识符,而只需使用dataSource.snapshot().sectionIdentifiers[sectionIndex]