【问题标题】:How do I got Multiple Selections in UICollection View using Swift 4如何使用 Swift 4 在 UICollection 视图中进行多项选择
【发布时间】:2018-10-11 10:13:02
【问题描述】:

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

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

【问题讨论】:

  • 您的other question 已关闭,因为它太宽泛了,这基本上是重复的。我们无法为您完成您的项目,也无法在互联网上为您搜索文章。 UICollectionView 中的多项选择已在此站点上多次回答,因此请进行一些研究。发布具体问题,解释问题并发布您的代码,我们将尽力提供帮助
  • @Scriptable 好的。谢谢。

标签: ios swift uicollectionview


【解决方案1】:

嗯,在 UICollectionView 中处理多个选择的最佳方式

  1. 启用多选
myCollectionView.allowsMultipleSelection = true
  1. 将此代码放入您的单元格awakeFromNib
override func awakeFromNib() {
  super.awakeFromNib()
        
  let view = UIView(frame: bounds)
  self.backgroundView = view
        
  let coloredView = UIView(frame: bounds)
  coloredView.backgroundColor = UIColor.red
  self.selectedBackgroundView = coloredView
}
  1. 可以获取选中的indexPath
let items = myCollectionView.indexPathsForSelectedItems

【讨论】:

  • 完美运行!!感谢您为我节省了一些麻烦:)
【解决方案2】:

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

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

如果没有,则在indexArray 中添加选定的单元格indexPath,并在valueArray 中添加选定的单元格值。

如果先前选择了当前选定的单元格,则从 indexArray 中删除 indexPath 并从 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

//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()
    }
}

【讨论】:

  • 它很旧...但我需要指出一些东西,检查是否在cellForItemAt 中选择了单元格不好,而是预先选择例如viewDidLoad 并使用selectItem(at:::),也可以使用这种方法不需要不必要的 layoutSubviews 和 reloadData。
【解决方案3】:

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

yourCollectionViewName.allowsMultipleSelection = true   

然后你可以这样做以查看选定的单元格 -

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

要取消选择你可以这样做 -

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

【讨论】:

    【解决方案4】:

    嗯,要实现这样的事情,你主要需要执行以下任务

    1. 每当用户点击特定单元格时,您需要在didSelectItemAtUICollectionView 委托方法中更改该item 的背景颜色

    2. 现在要将数据发送到服务器,您需要一个array 来存储所有选定的单元格,然后将该数组发送到服务器。您也可以在didSelectItemAt 方法中执行相同的操作

    我可以向您展示该函数的原型:

    假设您有一个名为 arrayForPopulating 的数组用于在 Collection View 中填充数据,并且我们有一个名为 finalSelections 的数组,其中包含用户所做的所有选择的名称

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
       let cell = collectionView.cellForItem(at: indexPath)
    
       // Change the background colour of the cell here
       cell.contentView.backgroundColor = UIColor.red
    
       // Add the selected cell's data to the array
       finalSelections.append(arrayForPopulating[indexPath.row])
    
    }
    

    现在您可以将finalSelections 数组发送到服务器!

    【讨论】:

    • 如果用户取消选择选定的单元格,如何从数组 [finalSelections] 中删除选定的单元格。
    • 使用 "indexPath.row" 获取该单元格项目的名称,然后检查该字符串或单元格项目的名称是否存在于数组 [finalSelections] 中,如果它确实获取该字符串或单元格项目的索引使用预定义的函数命名,然后删除该索引位置的元素!删除后,重新加载集合视图,以便单元格恢复正常外观!
    猜你喜欢
    • 2015-11-07
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多