我试图用黄色边框在 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 个空像素,因此它应该可以工作,但实际上没有。
它正在记录“你好”,但边框保持黑色。看这个截图:
最佳答案
你获取单元格的方式不对
FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];
将使当前未使用的单元格出列或分配具有指定标识符的新单元格。
使用
FilterCell *filterCell = (FilterCell *)[collectionView cellForItemAtIndexPath:indexPath];
相反。
无论如何,更简洁的解决方案是设置单元格的 backgroundView
和 selectedBackgroundView
属性,而不触及 backgroundProperty
颜色(将保留clear
默认)。通过这种方式,您可以避免委托(delegate)方法并实现相同的行为。
关于ios - 选中时突出显示集合单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866075/