【发布时间】:2016-07-27 14:52:24
【问题描述】:
我有一个UICollectionView,标题下有多个单元格,没什么特别的。
这是,我想让 collectionView 可以打开和关闭。加载视图时,该部分将关闭,当您点击标题时,所有单元格都将显示(带有常规的打开动画)。
有人知道我该怎么做吗?
谢谢!
【问题讨论】:
标签: ios objective-c swift cocoa-touch uicollectionview
我有一个UICollectionView,标题下有多个单元格,没什么特别的。
这是,我想让 collectionView 可以打开和关闭。加载视图时,该部分将关闭,当您点击标题时,所有单元格都将显示(带有常规的打开动画)。
有人知道我该怎么做吗?
谢谢!
【问题讨论】:
标签: ios objective-c swift cocoa-touch uicollectionview
有几种不同的方法可以做到这一点,但一种简单的方法是跟踪展开的部分
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)
}
如果你想清理一下
【讨论】:
您可以通过插入和删除单元格来做到这一点。
用于此
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];
【讨论】: