UICollectionView error due to highlighting problem

I have a UICollectionView on iOS7 that accidentally crashes when scrolling intensively. I turned on the zombies and found that this gives me an error:

 *** -[NSIndexPath section]: message sent to deallocated instance 0x17dbc970 

I believe this is due to the Apple error described here . Apparently, the application crashes when someone selects a cell while scrolling quickly, and then the OS tries to highlight it when it exits the screen, when it ceases to exist. The proposed solution is to disable the userInteractionEnabled property of the cell, and then handle the selection using UIGestureRecogniser .

Has anyone else encountered this same problem? Also, I tried disabling the userInteractionEnabled property and using a gesture recognizer, but this does not seem to work. Any idea how I can fix this?

EDIT : code added on request

 -(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *CellIdentifier = @"Gallery_Cell"; GalleryCell *cell= (GalleryCell *)[self.flowCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; if (indexPath.row < self.collectionData.count) { CellDetails *dets = [self.collectionData objectAtIndex:indexPath.row]; NSURL *mainImageURL = [NSURL URLWithString:dets.imageURL]; cell.image.contentMode = UIViewContentModeScaleAspectFill; cell.image.clipsToBounds = YES; if ([[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:[self cacheKeyForURL:mainImageURL]] == nil) { [cell.image setImageWithURL:mainImageURL placeholderImage:nil]; }else{ [cell.image setImage:[[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:[self cacheKeyForURL:mainImageURL]]]; } } return cell; } 

EDIT : more code ..

I defined GalleryCell for reuse as follows:

 [self.flowCollection registerNib:[UINib nibWithNibName:@"Thumbs_Cell" bundle:nil] forCellWithReuseIdentifier:@"Gallery_Cell"]; 

GalleryCell class implementation:

GalleryCell.h

 @interface GalleryCell : UICollectionViewCell @property (nonatomic, retain) IBOutlet UIImageView *image; @end 

GalleryCell.m

 @implementation GalleryCell @synthesize image; -(void) setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; [self setNeedsDisplay]; } -(void)prepareForReuse { [super prepareForReuse]; [self.image cancelCurrentImageLoad]; // SDWebImage method to cancel ongoing image load } 
+4
source share
2 answers

OK I think I solved it. In case someone encounters this problem, here is the fix:

In my UICollectionViewDelegate I implemented the following method:

 -(BOOL) collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ return NO; } 

This prevents the selection of cells and, therefore, avoids a crash when the system tries to turn it off when it goes off the screen. But, when you do this, it also stops calling the didSelectItemAtIndexPath method. Therefore, to implement cell selection, I had to use the UITapGestureRecogniser method.

Hope this helps.

+9
source

I would suggest returning to the following:

 - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ return !collectionView.dragging && !collectionView.tracking; } 
+1
source

Source: https://habr.com/ru/post/959481/


All Articles