Ios 如何创建打开/关闭collectionView部分
我有一个Ios 如何创建打开/关闭collectionView部分,ios,objective-c,swift,cocoa-touch,uicollectionview,Ios,Objective C,Swift,Cocoa Touch,Uicollectionview,我有一个UICollectionView,标题下有许多单元格,没有什么特别的 问题是,我想让collectionView可以打开和关闭。加载视图时,该部分将关闭,当您点击标题时,将显示所有单元格(带有常规打开动画) 有人知道我该怎么做吗 谢谢大家! 有几种不同的方法可以做到这一点,但一种简单的方法是跟踪哪些部分被展开 var expandedSections = NSSet() 然后在你的部分标题上设置一个手势识别器,告诉你什么时候被点击。当用户点击某个部分时,您需要两种方法: func se
UICollectionView
,标题下有许多单元格,没有什么特别的
问题是,我想让collectionView可以打开和关闭。加载视图时,该部分将关闭,当您点击标题时,将显示所有单元格(带有常规打开动画)
有人知道我该怎么做吗
谢谢大家! 有几种不同的方法可以做到这一点,但一种简单的方法是跟踪哪些部分被展开
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))
}
然后在NumberOfItems部分中执行以下操作:
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))
}
及
如果您想稍微清理一下,有几种不同的方法可以做到这一点,但一种简单的方法是跟踪展开的部分
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))
}
然后在NumberOfItems部分中执行以下操作:
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))
}
及
如果您想稍微清理一下,可以通过插入和删除单元格来完成 用于此
performbatchUpdate
块中的insertItemsAtIndexPaths
和deleteemsatindexpaths
方法如下:
[self.collectionView performBatchUpdates:^ {
[datasource insertObject:OBJECT atIndex:INDEX];
[self.collectionView insertItemsAtIndexPaths:@[INDEX_PATH]];
} completion:nil];
及
可以通过插入和删除单元格来完成此操作 用于此
performbatchUpdate
块中的insertItemsAtIndexPaths
和deleteemsatindexpaths
方法如下:
[self.collectionView performBatchUpdates:^ {
[datasource insertObject:OBJECT atIndex:INDEX];
[self.collectionView insertItemsAtIndexPaths:@[INDEX_PATH]];
} completion:nil];
及