【问题标题】:How to select all UICollectionView Cell's and Deselect by button click?如何选择所有 UICollectionView 单元格并通过单击按钮取消选择?
【发布时间】:2019-10-24 06:28:06
【问题描述】:

我需要实现一些具有超过 15 个按钮作为过滤选项卡的文件管理器, 我添加了水平 CollectionView、自定义 Cell、两个数组。 单选效果很好, 但我需要全选并通过单击按钮取消全选 知道如何实现吗?

Selecting all the items in UICollectionView iOS, even the cells that are not visible

这个不行


 var arraySelectedFilterIndex = [IndexPath]()
 var arraySelectedFilterData = [String]()

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.filterTitles.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterCell", for: indexPath) as! FilterCell

        cell.titleL.text = String(filterTitles[indexPath.row])
        cell.pointIMG.image = UIImage(named: filterTitles[indexPath.row] + "40")

        if arraySelectedFilterIndex.contains(indexPath) { // You need to check wether selected index array contain current index if yes then change the color
            cell.isSelected = true
        }
        else {
            cell.isSelected = false
        }

        cell.layoutSubviews()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
        Haptico.shared().generate(.light)

        let strData = filterTitles[indexPath.item]

        if arraySelectedFilterIndex.contains(indexPath) {
            arraySelectedFilterIndex = arraySelectedFilterIndex.filter { $0 != indexPath}
            arraySelectedFilterData = arraySelectedFilterData.filter { $0 != strData}
        }
        else {
            arraySelectedFilterIndex.append(indexPath)
            arraySelectedFilterData.append(strData)
        }
        collectionView.reloadData()
        print(arraySelectedFilterData)
    }

@IBAction func selectAllA(_ sender: Any) {. 

//here need add code with All Select and Deselect functions

}

【问题讨论】:

  • 除了all selected & all deselected还有其他状态吗?

标签: ios swift uicollectionview uicollectionviewcell jquery-ui-selectable


【解决方案1】:

我不确定为什么有时您使用“indexPath.row”,有时使用“indexPath.item”。

无论如何,你的函数应该是这样的

@IBAction func selectAllA(_ sender: Any) {
        arraySelectedFilterIndex.removeAll()
        arraySelectedFilterData.removeAll()

        for (index, element) in self.filterTitles.enumerated() {
            arraySelectedFilterIndex.append(IndexPath(item: index, section: 0))
            arraySelectedFilterData.append(element)
        }

        collectionView.reloadData()
        print(arraySelectedFilterData)
    }

首先您要清除以前的选择以避免重复,但您可以使用地图/字典结构来避免重复而不是列表。

然后为每个元素添加索引和数据到所选列表。最后重新加载数据,我把最后的打印留给你,你可以检查一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多