我的标题下有一个UICollectionView
个单元格,没什么特别的。
这就是说,我想让collectionView可以打开和关闭。加载视图后,该部分将关闭,当您点击标题时,将显示所有单元格(使用常规打开动画)。
有人知道我该怎么办?
谢谢!
答案 0 :(得分:0)
有几种不同的方法可以做到这一点,但一种简单的方法是跟踪扩展的部分
var expandedSections = NSSet()
然后在您的部分标题上设置一个手势识别器,告诉您何时点击它。当用户点击某个部分时,您需要两种方法:
func sectionHeaderWasTapped(section: Int) {
if self.expandedSections.contains(section) {
self.expandedSections.removeObject(Int)
}
else {
self.expandedSections.addObject(Int)
}
self.collectionView.reloadSections(NSIndexSet(index: section))
}
然后在numberOfItemsInSection中执行:
func numberOfItemsInSection(section: Int) {
if self.expandedSections.contains(section) {
return numberOfItemsInSection
}
else {
return 0
}
}
你可以创建帮助方法来清理它。例如:
func toggleSectionExpanded(section: Int) {
if self.expandedSections.contains(section) {
self.expandedSections.removeObject(Int)
}
else {
self.expandedSections.addObject(Int)
}
self.collectionView.reloadSections(NSIndexSet(index: section))
}
和
func sectionIsExpanded(section:Int) {
return self.expandedSections.contains(section)
}
如果你想稍微清理一下
答案 1 :(得分:0)
您可以通过插入和删除单元格来完成此操作。
用于此
insertItemsAtIndexPaths
块中的deleteItemsAtIndexPaths
和performBatchUpdates
方法如下所示:
[self.collectionView performBatchUpdates:^ {
[datasource insertObject:OBJECT atIndex:INDEX];
[self.collectionView insertItemsAtIndexPaths:@[INDEX_PATH]];
} completion:nil];
和
[self.collectionView performBatchUpdates:^ {
[datasource removeItemAtIndex:INDEX_PATH];
[self.collectionView deleteItemsAtIndexPaths:@[INDEX_PATH]];
} completion:nil];