我有一个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
})
}
}
答案 0 :(得分:0)
在numberOfSections函数中返回2 然后在cellforitemat函数中使用indexpath.section上的开关
答案 1 :(得分:0)
好的。所以我认为经过各种试验,我已经解决了这个问题。这是万一其他人遇到此问题的最终解决方案。
基本思想是:
可以将相反的想法用于合并各节。
这是解决方案:
func splitSection(at index: IndexPath) {
# first get a list of deleted and inserted indices as a result of splitting
let (deleted, inserted) = self.document.splitSection(at: index)
if let cv = self.collectionView {
cv.performBatchUpdates({
let N = self.document.numberOfSection
// now move over the sections to make space for one more section.
for idx in index.section+1..<N-1 {
cv.moveSection(idx, toSection: idx+1)
}
// add a new section
cv.insertSections(IndexSet.init(integer: index.section+1))
// now perform item movements to finish splitting
cv.deleteItems(at: deleted)
cv.insertItems(at: inserted)
}, completion: { (_) in
self.document.updateSections()
})
}
}