Ast*_*pta 8 accordion ios uicollectionview uicollectionviewcell swift
我有一个包含多个部分的 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] |
| |
+------------------------+
Run Code Online (Sandbox Code Playgroud)
如果单击“A Header”的隐藏按钮,设计将如下所示:
+------------------------+
| A Header [SHOW] |
| -----------------------|
| B Header [HIDE] |
| [Cell A] [Cell B] |
| [Cell c] [Cell D] |
+------------------------+
Run Code Online (Sandbox Code Playgroud)
我读过手风琴菜单,但它似乎与 TableView 一起使用。快速制作简单的手风琴 TableView?
我还尝试重新加载 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;
}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到 NSInternalInconsistencyException - Invalid update: invalid number of items in section 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] |
| |
+------------------------+
Run Code Online (Sandbox Code Playgroud)
如果单击标题 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] |
| |
+------------------------+
Run Code Online (Sandbox Code Playgroud)
您必须添加数据源方法。在该方法中您可以设计页眉或页脚视图。
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView
let headerBtn = UIButton()
headerBtn.addTarget(self, action:#selector(headerBtnTapped), for: .touchUpInside)
self.view.addSubview(headerBtn)
return headerView
case UICollectionElementKindSectionFooter:
let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView
return footerView
default:
assert(false, "Unexpected element kind")
}
}
Run Code Online (Sandbox Code Playgroud)
在标题视图中,headerBtnTapped
方法将在 headerView 上按下 headerBtn 后调用。因此您可以在方法中切换数组或内容(您返回的内容numberOfItemsInSection
)headerBtnTapped
并重新加载它。
归档时间: |
|
查看次数: |
3805 次 |
最近记录: |