Show number of Swift CollectionView taps

Asked 2 years ago, Updated 2 years ago, 94 views

I would like to know how to count the number of taps per cell when I tap CollectionViewCell on Swift and display them in each cell.The count is from the start to the end of the application. How should I write it in this code?
I'm still a beginner, so I think it's hard to understand the questions, but I appreciate your cooperation.

`// Method called upon cell selection

 func collectionView(collectionView:UICollectionView, didSelectItemAtIndexPath:NSIndexPath){
    let testCell=collectionView.cellForItemAtIndexPath(indexPath)as! SekiCollectionViewCell

`

swift uicollectionview

2022-09-30 19:53

1 Answers

I'll give you a quick example.(Described as Swift3)

In addition to the methods mentioned by the questioner, try to have the following properties:
It doesn't have to be a Dictionary type, it's just a simple example.

// Variable that stores the number of taps per cell
private tartapCount = [IndexPath:UInt]()

functioncollectionView(_collectionView:UICollectionView, didSelectItemAtindexPath:IndexPath){    
    let before = self.tapCount [indexPath] ???0
    let after=before+1

    self.tapCount [indexPath] = after

    print("cell at [section:\(indexPath.section), index:\(indexPath.row)] was pasted\(after) times intotal.")
}

This time, the number of taps is output in the log, but for the convenience of the questioner
Please use it to view it.

This example cannot be used when the number or placement of cells changes.
The preferred method is to let the cell itself have the number of taps.


2022-09-30 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.