我有一个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;
}
答案 0 :(得分:1)
更好的方法是使用didSelect和didDeselect代理。当用户选择单元格时,抓取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"]];
}
如果用户选择另一个单元格,换句话说deselect选择上一个单元格,您可以使用更改单元格。 didDeselectItemAtIndexPath委托
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *deSelectedCell = [collectionView cellForItemAtIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[deSelectedCell viewWithTag:101];
imageView.image = nil;
}
答案 1 :(得分:0)
执行reloadItemsAtIndexPaths:代替,然后在cellForItemAtIndexPath中,检查[[collectionView indexPathsForSelectedItems] containsObject:indexPath]如果为true,则只需在其中更改单元格的属性。
我希望它将对您有所帮助。谢谢。