如何使用 Swift 4 在 UICollection 视图中获得多个选择

7 ios uicollectionview swift

我是 swift 的新手,并且从头开始构建 iOS 应用程序(使用 swift 4),并且想要做类似下面的事情。1.在UICollectionView中实现多单元格选择,2.将选中的单元格数据传递给Server。请任何人都可以帮助我,如何做到这一点?告诉我这样做的过程和支持文章。

下面是参考图片。提前致谢。

参考图像/屏幕

ben*_*oud 27

好吧,在 UICollectionView 中处理多项选择的最佳方式

1) 启用多选

myCollectionView.allowsMultipleSelection = true
Run Code Online (Sandbox Code Playgroud)

2) 在自定义单元格类中覆盖 isSelected var

override var isSelected: Bool {
        didSet {
            if self.isSelected {
                backgroundColor = UIColor.red
            }
            else {
                backgroundColor = UIColor.purple
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

3) 并且可以得到选中的 indexPath 项

let items = myCollectionView.indexPathsForSelectedItems
Run Code Online (Sandbox Code Playgroud)

  • 作品完美!谢谢你让我省去了一些麻烦:) (2认同)

Kul*_*eep 11

这个基本的例子。您可以根据您的数据进行更改。

当您选择任何单元格时,您需要检查所选单元格之前是否已被选中。

如果没有,那么添加选定单元格indexPathindexArray,并在选定单元格的值valueArray

如果先前选择了当前选定的单元格,则indexPath从中删除indexArray并从中删除选定的单元格值valueArray

continue按钮按下传递arrSelectedData到服务器或下一个屏幕。

定义以下 3 个数组。

var arrData = [String]() // This is your data array
var arrSelectedIndex = [IndexPath]() // This is selected cell Index array
var arrSelectedData = [String]() // This is selected cell data array
Run Code Online (Sandbox Code Playgroud)

//UICollectionView 委托 & 数据源

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout 
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.arrData.count
    }

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

        if arrSelectedIndex.contains(indexPath) { // You need to check wether selected index array contain current index if yes then change the color
            cell.vw.backgroundColor = UIColor.red
        }
        else {
            cell.vw.backgroundColor = UIColor.lightGray
        }

        cell.layoutSubviews()
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 100, height: 100)
    }

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

        let strData = arrData[indexPath.item]

        if arrSelectedIndex.contains(indexPath) {
            arrSelectedIndex = arrSelectedIndex.filter { $0 != indexPath}
            arrSelectedData = arrSelectedData.filter { $0 != strData}
        }
        else {
            arrSelectedIndex.append(indexPath)
            arrSelectedData.append(strData)
        }

        collectionView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 8

您可以编写这样的代码来启用多项选择:-

yourCollectionViewName.allowsMultipleSelection = true   
Run Code Online (Sandbox Code Playgroud)

那么你可以这样做来查看选定的单元格 -

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    var cell = collectionView.cellForItemAtIndexPath(indexPath)
    if cell?.selected == true {
        cell?.backgroundColor = UIColor.orangeColor()
    }
}
Run Code Online (Sandbox Code Playgroud)

取消选择你可以做这样的事情 -

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {        
   var cell = collectionView.cellForItemAtIndexPath(indexPath)
   cell?.backgroundColor = UIColor.clearColor()
}
Run Code Online (Sandbox Code Playgroud)