首页 > 解决方案 > 如何拆分 UICollectionView 中的部分?

问题描述

我有一个 UICollectionView,它实现了按部分组织的页面。我想将一个部分分成两部分并具有以下逻辑。但 UICollectionView 似乎对此并不满意:

func splitSection(at index: IndexPath) {
    // this performs the logical split
    self.document.splitSection(at: index)

    if let cv = self.collectionView {
      cv.performBatchUpdates({
        let N = self.document.numberOfSection
        let n = N - index.section - 1
        let range = Range.init(uncheckedBounds: (index.section, n))
        let affectedSections = IndexSet.init(integersIn: range)
        cv.reloadSections(affectedSections)

        let sections = IndexSet.init(integersIn: Range.init(uncheckedBounds: (N-1, 1)))
        cv.insertSections(sections)
      }, completion: { (_) in
        // commit section updates
      })
    }
  }

标签: iosswiftuicollectionview

解决方案


在 numberOfSections 函数中返回 2 然后在 cellforitemat 函数中使用 indexpath.section 上的开关


推荐阅读