【问题标题】:Swift make select/unselect in single/multiple selection mode for UICollectionView inside UITableViewCellSwift 在 UITableViewCell 内的 UICollectionView 的单选/多选模式下进行选择/取消选择
【发布时间】:2020-01-08 10:17:00
【问题描述】:

我是 iOS 新手,我想在 UITableView 中实现一个 UICollectionView,它可以在 UITableview 的第 1 部分中进行多个选择/取消选择。在第 2 节中,只允许进行一次选择。如果我关闭 Viewcontroller 并且当我再次打开它时,它必须将最后选择的单元格显示为突出显示,并保存选择状态事件。

我搜索了教程,但所有教程都没有提到选择/取消选择集合单元格的状态或在关闭视图控制器后保存状态。

有人可以帮忙实现吗?

提前致谢!

这是我到现在为止的代码:

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "clvCell", for: indexPath) as! demoCollectionViewCell
        cell.title.text = clvData[indexPath.item] as? String
        return cell

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = .red
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell?.backgroundColor = .white
}

你们也可以在这里查看我的项目: https://mega.nz/#!xRs0EQyQ

【问题讨论】:

  • 能否请您在此处发布一些您到目前为止所做的代码。
  • 我添加了我选择的代码并上传了我当前的演示项目。你可以检查一下。
  • 要进行多项选择,请查看here。并且对于单一选择,将数组替换为单个对象/数据而不是数组。
  • 那么,关闭视图控制器然后再次打开时保存选择状态如何?

标签: ios swift uitableview uicollectionview


【解决方案1】:

试试这个,如果您有任何问题或者这是否解决了您的问题,请告诉我。


    var arrSelectedIndex:[IndexPath] = []// store this array either in database by api or in local
    var clvData:[String] = []// your data array

    //get the arrSelectedIndex from default in viewDidLoad before reloading the table and collection. 
    override func viewDidLoad() {
        super.viewDidLoad()
        if let myArray = UserDefaults.standard.array(forKey: "selectedArray") as? [IndexPath] {
            arrSelectedIndex = myArray
        } else {
            arrSelectedIndex = []
        }
    }

    // and save the arrSelectedIndex in viewWillDisappear method
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UserDefaults.standard.set(arrSelectedIndex, forKey: "selectedArray")
    }

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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "clvCell", for: indexPath) as! demoCollectionViewCell
            cell.title.text = clvData[indexPath.item] as? String

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

            return cell

        }

        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath)
            cell?.backgroundColor = .red
            if !arrSelectedIndex.contains(indexPath) {// if it does not contains the index then add it
                arrSelectedIndex.append(indexPath)
            }
        }

        func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            let cell = collectionView.cellForItem(at: indexPath)
            cell?.backgroundColor = .white
            if let currIndex = arrSelectedIndex.firstIndex(of: indexPath) {// if it contains the index then delete from array
                arrSelectedIndex.remove(at: currIndex)
            }
        }

【讨论】:

  • 我试过你的建议,效果很好。但是还有一个要求是在关闭后保存集合视图的选定状态。再次打开时必须突出显示。我目前正在使用UserDefaults 来保存arrSelectedIndex,但它有一个错误。您可以查看此 gif 以获取更多详细信息:media.giphy.com/media/S8B5Ajtg7zcYkvPsdg/giphy.gif
  • 我看不到 gif,你能在这里写下你发现了什么错误吗?顺便说一句,我已经更新了答案,看看那个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2014-04-20
  • 1970-01-01
  • 2017-03-17
  • 2019-12-03
相关资源
最近更新 更多