I have a UICollectionView which contains a UIImageView and is only one row. I am trying to change the image to a selected image when tapped but the image will not update for some reason. The UIImage is set when the UICollectionView calls:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
but it will not update when it calls:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Here is the code, as you can see I already tried setNeedsDisplay , reloadData, setImage and nullifying the UImageView itself, not setting it in the cellForItemAtIndexPath ... None of it has worked.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell;
UIImage *curImage;
static NSString *cellAvatarMediaIdentifier = @"cvAvatarCell";
cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellAvatarMediaIdentifier forIndexPath:indexPath];
Child *currentChild = [self.children objectAtIndex:indexPath.row];
curImage = [UIImage imageNamed:@"male.png"];
UIImageView *childSilhouetteView = (UIImageView *)[cell viewWithTag:300];
if (curImage != nil)
{
// this WORKS !!!!
[childSilhouetteView setImage:curImage];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *selectedImage;
Child *currentChild = [self.children objectAtIndex:indexPath.row];
UIImageView *childSilhouetteView = (UIImageView *)[collectionView viewWithTag:300];
selectedImage = [UIImage imageNamed:@"male_selected.png"];
if (selectedImage != nil)
{
// This does NOT work, image is never updated
NSLog(@"Before setting image");
childSilhouetteView = nil;
[childSilhouetteView setImage:selectedImage];
[childSilhouetteView setNeedsDisplay];
[collectionView reloadData];
}
}