iosobjective-cuicollectionviewuicollectionviewcelluicollectionviewdelegate

UICollectionView didSelecteItem Delegate behaving weirdly


I have a UICollectionView to show some components. Its vertical scrolling and with only 1 column.

I am adding 2 same objects but with different data to collectionView's datasource at different sections.

And in my cell class my initialisation and update methods are as follow

- (void)commonInitializer {

    self.backgroundColor = kVTColorWhite;

    self.imageView = [[VTUIKit imageSquareXL2] addToSuperview:self];
    self.imageView.f_x = kVTPadding;
    self.imageView.backgroundColor = kVTColorBackground;
    self.imageView.f_width = [VTAdSpaceDisplayCell cellSize].width - 2*kVTPadding;
    self.imageView.f_height = [VTUIKit screenWidth]/3.125;
    self.imageView.layer.cornerRadius = 2.0;
    self.imageView.layer.masksToBounds = YES;
}


- (void)updateWithModel:(id)model{

    self.model = model;

    if (self.model.image == nil) {
        [self updateEmptyCells];
    }
    else{
        [self.imageView setImageWithUrl:self.model.image optimizedForSize:[self class] cellSize] withPlaceholder:[UIImage imageNamed:@"Placeholder-Image"]];
    }
}

But collectionView's didSelectItemAtIndexPath for this cell is being called sometimes and sometimes not, its very weird.

I have also set collectionView's delegate to current class and I am also not adding any gesture recogniser to cell or collectionView.

But still the behaviour is strange and very random, I cant even get a particular scenario to debug.

Any help would be much appreciated.

Thanks


Solution

  • The actual problem is nowhere related to UIImageView or UICollectionViewCell.

    Earlier I was using [self.collectionView reloadSections:[INDEX]] for reloading collectionView's sections and that was creating some problems and I dont know why.

    But, I changed them to [self.collectionView reloadData] and the issue is gone.

    Very strange and weird reason for issue to appear and I have no explanation even though I searched for it.

    Anyways, I got my issue resolved so I am posting my solution as an answer if that helps anyone in future.

    Thanks