【问题标题】:Collapse cells of a section in UICollectionView swift在 UICollectionView swift 中折叠部分的单元格
【发布时间】: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


【解决方案1】:

您必须添加数据源方法。在该方法中您可以设计页眉或页脚视图。

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")
        }
    }

在 header 视图中,headerBtnTapped 方法将在 headerView 上按下 headerBtn 后调用。因此您可以在headerBtnTapped 方法中切换数组或内容(在numberOfItemsInSection 返回)并重新加载它。

【讨论】:

  • 感谢您的回答,但这不是我想要的。我的问题是方法 - numberOfItemsInSection,而不是用户点击时如何触发方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多