如何更改 UICollection UIImageView 中的 UIimage?
How to change UImage in UICollection UIImageView?
我有一个 UICollectionView,其中包含一个 UIImageView,并且只有一行。我试图在点击时将图像更改为选定图像,但由于某种原因图像不会更新。当 UICollectionView 调用时设置 UIImage:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
但调用时不会更新:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
这是代码,如您所见,我已经尝试过 setNeedsDisplay、reloadData、setImage 并使 UImageView 本身无效,而不是在 cellForItemAtIndexPath 中设置它...None 它已经起作用。
- (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:
中找到对应的child,目前您正在使用标签访问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 将被重绘,因此您将在视图中看到默认图像。
首先要访问特定单元格的项目,首先需要获取单元格。
检查这个答案:
UICollectionView didSelectRowAtIndexPath doesn't work
其次确保编译器进入您正在检查图像是否为 nil 的状态。确保 "male_selected.png" 存在于同名的资产文件夹中。
第三步是删除所有不必要的代码。那你可以走了
我有一个 UICollectionView,其中包含一个 UIImageView,并且只有一行。我试图在点击时将图像更改为选定图像,但由于某种原因图像不会更新。当 UICollectionView 调用时设置 UIImage:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
但调用时不会更新:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
这是代码,如您所见,我已经尝试过 setNeedsDisplay、reloadData、setImage 并使 UImageView 本身无效,而不是在 cellForItemAtIndexPath 中设置它...None 它已经起作用。
- (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:
中找到对应的child,目前您正在使用标签访问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 将被重绘,因此您将在视图中看到默认图像。
首先要访问特定单元格的项目,首先需要获取单元格。
检查这个答案: UICollectionView didSelectRowAtIndexPath doesn't work
其次确保编译器进入您正在检查图像是否为 nil 的状态。确保 "male_selected.png" 存在于同名的资产文件夹中。
第三步是删除所有不必要的代码。那你可以走了