Lester
Lester

Reputation: 731

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

Upvotes: 4

Views: 2003

Answers (3)

In AddViewController you are adding gesture recognizer to the view, which make every user gesture to be detected by the tap recognizer. You can remove this gesture:

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddViewController.dismissKeyboard))

For dismissing keyboard you can implement tebleView delegate method:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dismissKeyboard()
    }

Upvotes: 0

Lester
Lester

Reputation: 731

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

Upvotes: 4

AntiVIRUZ
AntiVIRUZ

Reputation: 342

Try to deselect this checkmark in Collection View properties

enter image description here

Upvotes: 5

Related Questions