我有一个UICollectionView,它包含一个UIImageView并且只有一行。 我试图在点击时将图像更改为选定图像,但由于某些原因图像不会更新。 UIImageView在UICollectionView调用时设置:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath但它在调用时不会更新:
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath这里是代码,你可以看到我已经尝试过setNeedsDisplay,reloadData,setImage并取消UImageView本身,而不是在cellForItemAtIndexPath中设置它...没有任何工作。
- (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]; } }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 *)indexPathbut it will not update when it calls:
(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPathHere 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]; } }最满意答案
您还需要在didSelectItemAtIndexPath:找到相应的子项didSelectItemAtIndexPath: ,当前您正在使用标记访问collectionView的视图。
- (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]; } }如果你调用reloadData ,你的collectionView将被重画,所以你会在你的视图中看到默认的图像。
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.
更多推荐
发布评论