我试图突出显示UICollectionView中具有黄色边框的选定集合单元格,以便用户可以看到当前选择了哪一个 . 我试过这个:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];
filterCell.window.backgroundColor = [UIColor yellowColor];
filterCell.backgroundColor = [UIColor yellowColor];
NSLog(@"hello");
}
在UICollectionViewCell内的UIImageView周围有2个空像素,所以它应该可以工作,但事实并非如此 .
它正在记录“你好”,但边框保持黑色 . 看这个截图:
4 回答
这可能对您有所帮助:
你以错误的方式得到了牢房
将使现在未使用的单元格出列或者分配具有指定标识符的新单元格 .
使用
代替 .
无论如何,更清洁的解决方案是设置单元格的
backgroundView
和selectedBackgroundView
属性,而不触及backgroundProperty
颜色(默认情况下将保留clear
) . 通过这种方式,您可以避免委托方法并实现相同的行为 .做一个
reloadItemsAtIndexPaths
:然后在cellForItemAtIndexPath中检查是否[[collectionView indexPathsForSelectedItems] containsObject:indexPath]
如果为true,则更改单元格的属性 .