I'm implementing a collection view in Xcode 11 project, what happens is that only second tap gets registered. When user taps on left cell it doesn't open detail screen and when user next taps on right cell then left cell opens, very strange behavior. I'm not using storyboards just programmatic approach. Here's my code:
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
return cv
}()
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 150, height: 150)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return myData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
cell.backgroundColor = .white
cell.data = myData[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let newViewController = SecondViewController(data: myData[indexPath.item])
self.navigationController?.pushViewController(SecondViewController, animated: true)
}
Replace ( didDeselectItemAt
isn't didSelectItemAt
)
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
with
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
Also it should be
let newViewController = SecondViewController(data: myData[indexPath.item])
self.navigationController?.pushViewController(newViewController, animated: true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.