Reputation: 1198
I am using a UICollectionView
to display some data. I am having one button on each cell. Now on the button click of at some specific index(ex: 9) I want to change buttons image for all cells. Can some one suggest how to do that?
Upvotes: 0
Views: 660
Reputation: 5695
We can simply add the logic in cellForItemAtIndexPath
like this.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (self.buttonClicked) { // boolean check
[cell.newButton setImage:buttonClickedImage];
}else {
[cell.newButton setImage:buttonNotClickedImage];
}
return cell;
}
After clicking the button, you can also just reload all visible cells.
[self.collectionView reloadItemsAtIndexPaths:self.collectionView.indexPathsForVisibleItems];
Feel free to suggest edits to make this answer better :)
Upvotes: 0
Reputation: 1349
I would loop through all the visible cells and change the image. For example:
for (CustomCellClass *cell in collectionView.visibleCells) {
[cell.button setImage:[UIImage imageNamed:@"new image"] forState:UIControlStateNormal];
}
For off-screen cells, I would make sure to set the new image in collectionView:cellForItemAtIndexPath:
as the user scrolls to those cells.
Upvotes: 1