Reputation: 297
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:
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];
}
}
Upvotes: 0
Views: 149
Reputation: 984
First of all to access the items of a particular cell, first you need to get the cell.
Check this answer: UICollectionView didSelectRowAtIndexPath doesn't work
Secondly make sure compiler is going into the condition where you are checking the image is nil or not. Make sure "male_selected.png" exists in your assets folder with same name.
Third step is to remove all unnecessary code. You are good to go then
Upvotes: 0
Reputation: 1933
You also need to find the corresponding child in didSelectItemAtIndexPath:
, currently you're accessing the collectionView
's view with tag.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *selectedImage;
Child *currentChild = [self.children objectAtIndex:indexPath.row];
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
UIImageView *childSilhouetteView = (UIImageView *)[cell 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];
}
}
If you call reloadData
, your collectionView will be redrawn so you will see the default images on your views.
Upvotes: 0