简体   繁体   English

Swift 4-如果已经选择,则取消选择集合视图单元格

[英]Swift 4 - deselect collection view cell if already selected

I have a collection view where users can select multiple cells and send the array of index to a server for saving their selection. 我有一个集合视图,用户可以在其中选择多个单元格并将索引数组发送到服务器以保存其选择。

everything is working except that when the collection view is created the selected items needs to be clicked two times in order to deselect them. 一切正常,除了创建集合视图时,需要单击两次选定的项目以取消选择它们。

How can I solve this issue of double clicking ? 如何解决这个双击问题?

extension ThirdViewController: UICollectionViewDelegate, UICollectionViewDataSource {


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return categoryList.count
    }

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

        cell.categoryLbl.text = categoryList[indexPath.row]

        if Defaults.hasKey(.categoryListIndices) {
            if (Defaults[.categoryListIndices]?.contains(indexPath.row))! {
                cell.alpha = 0.1
                cell.isSelected = true
            } else {

                cell.alpha = 1

            }
        }

        return cell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! UserCategoryCollectionViewCell


        cell.alpha = 0.1

    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let deselectedCell = collectionView.cellForItem(at: indexPath) as? UserCategoryCollectionViewCell
        deselectedCell?.alpha = 1

        print(deselectedCell?.isSelected)
        collectionView.deselectItem(at: indexPath, animated: false)
    }

}

first of all. 首先。 add this to your viewDidLoad. 将此添加到您的viewDidLoad中。 collectionView?.allowsMultipleSelection = true collectionView?.allowsMultipleSelection = true

and then add these two functions. 然后添加这两个功能。

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell=collectionView.cellForItem(at: indexPath)
    collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
    cell?.backgroundColor = Color.hexStringToUIColor(hex: "#F5A331")
    let lbl = cell?.subviews[1] as! UILabel
    lbl.textColor = Style.cellHighlightedColor
}
 override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    collectionView.deselectItem(at: indexPath, animated: true)
    let cell=collectionView.cellForItem(at: indexPath)
    collectionView.deselectItem(at: indexPath, animated: true)

    cell?.backgroundColor = UIColor.white
    let lbl = cell?.subviews[1] as! UILabel
    lbl.textColor = UIColor.darkGray
}
This will do

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM