【发布时间】:2017-09-26 06:42:12
【问题描述】:
我有一个包含多个部分的 UICollectionView。每个部分都有一个 HeaderView(UICollectionReusableView 类型)和多个单元格(UICollectionViewCell 类型)。
每个标题都有一个隐藏/显示按钮,用于隐藏/显示单元格。即使标题下的所有单元格都折叠/隐藏,标题在任何时候都不会隐藏。
+------------------------+
| A Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| -----------------------|
| B Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| |
+------------------------+
如果点击“A Header”的隐藏按钮,设计将如下所示:
+------------------------+
| A Header [SHOW] |
| -----------------------|
| B Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
+------------------------+
我阅读了有关 Accordion Menu 的信息,但它似乎与 TableView 一起使用。 Making Simple Accordion TableView in swift?
我还尝试重新加载 0 个单元格以复制隐藏行为
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
isFirstHidden = true
collectionView.performBatchUpdates({
let indexSet = IndexSet(integer: 0)
collectionView.collectionViewLayout.invalidateLayout()
collectionView.reloadSections(indexSet)
}, completion: nil)
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if (section == 1 && isFirstHidden) {
return 0;
}
return 4;
}
但我仍然收到 NSInternalInconsistencyException - 无效更新:第 1 节中的项目数无效。
您能否给我任何指示或分享文档链接,这将有助于我了解单元格的折叠行为如何工作。
编辑: 我想要实现的另一个示例。
+------------------------+
| A Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| -----------------------|
| B Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| -----------------------|
| C Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| |
+------------------------+
如果点击了 Header B 的隐藏按钮 -
+------------------------+
| A Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| -----------------------|
| B Header [SHOW] |
| -----------------------|
| C Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
| |
+------------------------+
【问题讨论】:
-
你不应该检查
section == 0 && isFirstHidden而不是section == 1吗?在您的示例中,Section 0将有0项目,Section 1将有 4 个项目(隐藏Section 0之后)。部分是从 0 开始的,而不是从 1 开始的。 -
嘿!我知道部分是基于 0 的事实。我想隐藏第 1 节而不是第 0 节的单元格。每当我尝试通过返回 0 来隐藏第 1 节的元素时,应用程序就会崩溃。但是,如果我对第 0 部分执行相同操作,则不会引发异常并且显示符合预期。我将通过添加一个示例来编辑问题
-
您的附加示例更好,但当您使用
IndexSet(integer: 0)时,您的代码仍会重新加载第 0 节。我过去写过类似的代码,我刚刚审查过它。当我关闭部分时,我使用self.collectionView?.deleteItems(at:)(并传递所有要删除的项目的IndexPaths),当我打开部分时,我使用self.collectionView?.reloadSections()(并传递IndexSet中的所有部分)而不是使用reloadSections()来添加和删除项目。
标签: ios swift uicollectionview accordion uicollectionviewcell