Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collection view cell slow detect selected item in swift

I'm putting the collection view into table view cell and I made it can display on the cell but when I want to select the collection cell (to change the color or print cell number), the select function is not working, I need to tap the cell many time to make it selected. Why the cell slow detect the selected item? What code will affect the cell to be selected?

This is the code for select the collection cell

override func awakeFromNib() {
    super.awakeFromNib()

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let width = UIScreen.main.bounds.width
    layout.scrollDirection = .vertical
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: width/5, height: width/4)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    collectionView?.collectionViewLayout = layout
    collectionView?.delaysContentTouches = false

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CategoryCollectionViewCell
    cell.cateImg.image = imageName[indexPath.row]
    cell.cateLabel.text! = nameArray[indexPath.row]
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
        cell.cateImg.image = imageName2[indexPath.row]
        print("collectionViewCell selected \(indexPath)")
    }
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    if let cell = collectionView.cellForItem(at: indexPath) as? CategoryCollectionViewCell {
        cell.cateImg.image = imageName[indexPath.row]
    }
}

enter image description here

Project zip link: https://www.dropbox.com/s/y10dgp3q61pi5n1/Finnciti.zip?dl=0

problem on AddViewCell.swift

like image 273
Lester Avatar asked Nov 05 '25 14:11

Lester


2 Answers

Try to deselect this checkmark in Collection View properties

enter image description here

like image 139
AntiVIRUZ Avatar answered Nov 08 '25 11:11

AntiVIRUZ


I fixed the problem after delete this code on AddViewController.

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddExpenseVC.dismissKeyboard))
view.addGestureRecognizer(tap)

@objc func dismissKeyboard() {
    view.endEditing(true)
}
like image 25
Lester Avatar answered Nov 08 '25 11:11

Lester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!