gpt4 book ai didi

ios - 如何突出显示 UICollectionView 单元格中的图像?

转载 作者:行者123 更新时间:2023-11-29 12:25:23 25 4
gpt4 key购买 nike

我有一个 collectionView,每个单元格都包含一个图像。现在我想突出显示图像,使其在您选择时像 iPhone 应用程序图标一样突出显示。

我试过这段代码,但它不起作用,它只是突出显示了背景。

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{ Cell* cell = (Cell*)[collectionView cellForItemAtIndexPath:indexPath];
cell.img.tintColor = [UIColor colorWithWhite:0 alpha:0.5 ];
return YES;
}

最佳答案

更好的方法是使用 didSelect 和 didDeselect 委托(delegate)。当用户选择单元格时,抓取 collectionView 单元格中的 ImageView 并根据需要更改任何属性。

示例代码。

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[selectedCell viewWithTag:101];
[imageView setImage:[UIImage imageNamed:@"whatever-image-you-want"]];

}

如果用户选择另一个单元格,换句话说,取消选择您可以更改单元格使用的前一个单元格。 didDeselectItemAtIndexPath 委托(delegate)

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {


UICollectionViewCell *deSelectedCell = [collectionView cellForItemAtIndexPath:indexPath];

UIImageView *imageView = (UIImageView *)[deSelectedCell viewWithTag:101];
imageView.image = nil;

}

关于ios - 如何突出显示 UICollectionView 单元格中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com