Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift how to deselect Collection Cell

TenViewController code :

class TenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
        var selectedCell: UICollectionViewCell!
        var arrayLocation = ["aaa", "bbb", "ccc", "ddd", "eee"]
        var myCollectionView: UICollectionView!
        func numberOfSections(in collectionView: UICollectionView) -> Int
            {
                return 1
            }
            func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int
            {
                return arrayLocation.count
            }

collection delegate

            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
                cell.backgroundColor = UIColor.white
                cell.titleLabel?.text = arrayLocation[indexPath.row]
                cell.titleLabel.font = UIFont.systemFont(ofSize: 24)
                return cell
            }
            // check cell and do somthing
            func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath)
            { selectedCell = myCollectionView.cellForItem(at: indexPath)! selectedCell.contentView.backgroundColor = UIColor.red
               collectionView.allowsSelection = true
                collectionView.allowsMultipleSelection = true
        }

I trying to use this func but it doesn't work

            func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath) {
            myCollectionView.deselectItem(at: indexPath, animated: false)  }

override func viewDidLoad() {
      //get view size
      let fullsize = UIScreen.main.bounds.size
      //get collectionViewCell layout
      let layout = UICollectionViewFlowLayout()
      //cell size     
      layout.itemSize = CGSize(width: 120, height: 30)
      //mycollectionView size 
            myCollectionView = UICollectionView(frame: CGRect(x: 108, y: 70, width: fullsize.width - 70, height: fullsize.height - 180), collectionViewLayout: layout)
      myCollectionView.delegate = self
      myCollectionView.dataSource = self
      myCollectionView.backgroundColor = UIColor.white
      myCollectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
          layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 50);
          layout.minimumLineSpacing = 10
            layout.minimumInteritemSpacing = 0.5
            myCollectionView.backgroundColor = UIColor(patternImage: myPag)
            self.view.addSubview(myCollectionView)

    }
            }

MyCollectionViewCell code

class MyCollectionViewCell: UICollectionViewCell {
       var titleLabel:UILabel!
        override init(frame: CGRect) {
            super.init(frame: frame)
            // create UILabel
            let w = Double(UIScreen.main.bounds.size.width)
            titleLabel = UILabel(frame:CGRect(x: 0, y: 0, width: w/3 - 10.0, height: 30))
            titleLabel.textAlignment = .center
            titleLabel.textColor = UIColor.black
            self.addSubview(titleLabel)
    }
required init?(coder aDecoder: NSCoder)
        {
            fatalError("init(coder:) has not been implemented")
        }
}

I would like to get cell deselect and what should I do?

Does anyone can help me plz thanks mate!!

how to make a button can chose all of cell?

like image 357
Jeff Avatar asked Nov 11 '16 04:11

Jeff


Video Answer


2 Answers

You need to set property allowsSelection and allowsMultipleSelection also in viewDidLoad instead of didSelectItemAt indexPath, Also you reusing the cell so your selection of cell is change when you scroll the CollectionView for preventing this issue create one instance of type [IndexPath] and access it in your collectionView's method like this.

var selectedCell = [IndexPath]()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
    cell.titleLabel?.text = arrayLocation[indexPath.row]
    cell.titleLabel.font = UIFont.systemFont(ofSize: 24)
    if selectedCell.contains(indexPath) {
        cell.contentView.backgroundColor = .red
    }
    else {
        cell.contentView.backgroundColor = .white
    }        
    return cell
}

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

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)!
    if selectedCell.contains(indexPath) {
        selectedCell.remove(at: selectedCell.index(of: indexPath)!)
        cell.contentView.backgroundColor = .white
    }
}
like image 100
Nirav D Avatar answered Oct 26 '22 14:10

Nirav D


Swift 3.0 Solution

extension UICollectionView {
    func deselectAllItems(animated: Bool = false) {
        for indexPath in self.indexPathsForSelectedItems ?? [] {
            self.deselectItem(at: indexPath, animated: animated)
        }
    }
}

How to use?

myCollectionView.deselectAllItems(false)
like image 29
Sourabh Sharma Avatar answered Oct 26 '22 14:10

Sourabh Sharma