如何使用 Swift 4 在 UICollection 视图中进行多项选择 [英] How do I got Multiple Selections in UICollection View using Swift 4

查看:19
本文介绍了如何使用 Swift 4 在 UICollection 视图中进行多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to swift and building iOS Application from the scratch (using swift 4) and want to do something like below. 1. Implement Multiple cell selections in UICollectionView, 2. Pass selected cells data to Server. Please anyone can help me, how to do that? Tell me the process and supporting articles to do that.

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

Below is reference Image. Thanks in Advance.

推荐答案

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

This basic example. You can change as per your data.

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

When you select any cell then you need to check that selected cell is already selected before or not.

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

If not then add selected cell indexPath in indexArray and selected cell value in valueArray.

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

If current selected cell is previously selected then remove indexPath from indexArray and also remove selected cell value from valueArray

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

on continue button press pass arrSelectedData to server or next screen.

定义以下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 委托 &数据源

//UICollectionView Delegate & DataSource

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

这篇关于如何使用 Swift 4 在 UICollection 视图中进行多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆