如何更改UICollection UIImageView中的UImage?(How to change UImage in UICollection UIImageView?)

编程入门 行业动态 更新时间:2024-10-26 22:28:37
如何更改UICollection UIImageView中的UImage?(How to change UImage in UICollection UIImageView?)

我有一个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 *)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]; } }

最满意答案

您还需要在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.

更多推荐

本文发布于:2023-08-07 14:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464771.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   UICollection   UIImageView   UImage   change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!