I am puzzled because It is just a simple thing but doesn't work.
This is my checked list before write question at here.
UICollectionViewDelegate is registered
There is nothing on above than UICollectionView from "Debug View Hierarchy"
isUserInteractionEnabled
of UICollectionView and UICollectionViewCell is true
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.
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.
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.
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.
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
I changed the isUserInteractionEnabled
property of the subviews of Cell to false
. and it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With