Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView didSelectItemAt not being called

I am puzzled because It is just a simple thing but doesn't work.

This is my checked list before write question at here.

  1. UICollectionViewDelegate is registered

  2. There is nothing on above than UICollectionView from "Debug View Hierarchy"

  3. isUserInteractionEnabled of UICollectionView and UICollectionViewCell is true

  4. isAllowsSelection of UICollectionView is true

Is there anything else I should check out?

=====

class MyView: NibDesignable { // It's UIView with NibDesignableProtocol (https://github.com/mbogh/NibDesignable)
    @IBOutlet weak var collectionView: UICollectionView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.setup()
    }

    private func setup() {
        print("test - It's called.")

        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.isUserInteractionEnabled = true
        self.collectionView.isAllowsSelection = true
    }
}

extension MyView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath {
        print("test - It's not called")
    }
}

I pasted only the part relating to question because all of code is too long.

like image 332
Byoth Avatar asked Mar 09 '18 07:03

Byoth


People also ask

How does swift implement UICollectionView?

Select the Main storyboard from the file browser. Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices.

What is CollectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content. Layouts.

What is the use of datasource and delegate in UICollectionView in IOS?

This method asks the data source object to provide a supplementary view to display in the collection view. It asks the datasource object whether the specified item can be moved to another location in the collectionview. This method moves the specified item to the specified indexpath.


2 Answers

This can happen if you have a gestureRecognizer in your custom cell. this will override the didSelectItemAt delegate of the collectionView.

If this is your issue, then you can resolve it by identifying the gestureRecognizer and doing the following:

let tap = UITapGestureRecognizer(target: self, action: #selector(self.someViewInMyCellTapped(_:)))
someViewInMyCell.addGestureRecognizer(tap)

tap.cancelsTouchesInView = false
like image 183
alionthego Avatar answered Nov 16 '22 00:11

alionthego


I changed the isUserInteractionEnabled property of the subviews of Cell to false. and it works.

like image 40
Li Jin Avatar answered Nov 15 '22 23:11

Li Jin