swift - 如何删除 Collection View 中的部分

标签 swift uicollectionview sections

如何从 Controller View 中删除各个部分?我在标题中有一个按钮,所有内容都已连接。只是不确定如何编写 3 个不同部分的代码。

我的数据模型

var fire = [UIImages]
var water = [UIImages]
var air = [UIImages]

var fireLabel = [String]
var waterLabel = [String]
var airLabel = [String]

我的单元配置代码

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if indexPath.section == 0 {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

        cell.fire.image = fireImages[indexPath.row]
        cell.fireLabel.text = fireNames[indexPath.row]


    return cell

    } else if indexPath.section == 1 {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

        cell.water.image = waterImages[indexPath.row]
        cell.waterLabel.text = waterNames[indexPath.row]

        return cell

    } else {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

        cell.air.image = airImages[indexPath.row]
        cell.airLabel.text = airNames[indexPath.row]

        return cell

    }

}

这是我的按钮代码,它位于每个标题中。我想要做的是,当您单击此按钮时,它会删除整个部分。再次为每个。但我似乎无法让它发挥作用。

//Delete Section Button
@IBAction func deleteSectionButton(sender: UIButton) {

    //Section tag
    let section = sender.tag

    //Update data model
    fireImages.removeAtIndex(section)
    fireNames.removeAtIndex(section)

    self.collectionView?.deleteSections(NSIndexSet(index: section))

}

我收到此错误:

*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:无效的节数。更新后 Collection View 中包含的节数 (3) 必须等于更新前 Collection View 中包含的节数 (3),加上或减去插入或删除的节数(0 个插入,1 个已删除)。'

但是我不知道这意味着什么。

最佳答案

您可以在更新数据后重新加载 Collection View 。它将按比例将您的数据填充回 Collection View

self.collectionView.reloadData();

关于swift - 如何删除 Collection View 中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909710/

相关文章:

swift - 以快速的方式在快速单元测试中模拟静态类方法?

ios - 快速获取collectionView中心单元格的indexPath?

ios - 如何在 UICollectionView 上始终显示滚动条

ios - 未调用 UICollectionViewDelegate 和 UICollectionViewDataSource 方法

html - 进入下一节的 "row"有问题

ios - 使用部分时继续详细了解 UItableView

elf - 真的需要 ELF .notes 部分吗?

ios - 如何在 Storyboard中添加自定义 UIControl 类?

swift - Firebase Twitter oAuth 回调不适用于 Swift ios13

ios - 顶部带有文本框的 UICollectionView