Reputation: 6718
I applied gesture recognizer on particular UICollectionViewCell. It works fine. But after reloading the uicollectionview, this gesture recongizer is applied rest on collectionviewcell. Suppose i have 10 cells. I apply gesture recongizer on 1st, 3rd, 4th,6th,7th,9th cells. Rest of cells 2nd,5th,8th cells don't have gesture recognizers. It works perfect on first time. After reload the collection view, 2nd,5th,8th cells also have gesture recognizer but i don't want. How to solve this issue. Please help me.
Upvotes: 0
Views: 1962
Reputation: 244
You should always attach your gesture recognizers to the collection view itself—not to a specific cell or view. The UICollectionView class is a descendant of UIScrollView, so attaching your gesture recognizers to the collection view is less likely to interfere with the other gestures that must be tracked. In addition, because the collection view has access to your data source and your layout object, you still have access to all the information you need to manipulate cells and views appropriately.
Upvotes: 2
Reputation: 8928
This is because UICollectionView reuses cells that are not visible any more. (Suppose you have 100 cells and only 8 visible - UICollectionView will keep at least 8 cells initialized, others might be reused) mwthod:
– dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
Thus, in your method:
- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath
When you dequeue your cell, you should resetup gesture recognizers or remove if you don't need them
Upvotes: 3