ios - 选中时突出显示集合单元格

标签 ios objective-c uicollectionview uicollectionviewcell

我试图用黄色边框在 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 个空像素,因此它应该可以工作,但实际上没有。

它正在记录“你好”,但边框保持黑色。看这个截图:

enter image description here

最佳答案

你获取单元格的方式不对

FilterCell *filterCell = (FilterCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"FilterCell" forIndexPath:indexPath];

将使当前未使用的单元格出列或分配具有指定标识符的新单元格。

使用

FilterCell *filterCell = (FilterCell *)[collectionView cellForItemAtIndexPath:indexPath];

相反。

无论如何,更简洁的解决方案是设置单元格的 backgroundViewselectedBackgroundView 属性,而不触及 backgroundProperty 颜色(将保留clear 默认)。通过这种方式,您可以避免委托(delegate)方法并实现相同的行为。

关于ios - 选中时突出显示集合单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15866075/

相关文章:

objective-c - UIAlertController:supportedInterfaceOrientations 被递归调用

ios - 从屏幕边缘滑动到 UICollectionView

iOS - View Controller 响应来自 uiview 的数据更改

c# - iOS 与 .Net 应用程序通信

ios - 一次可以选择两次的自定义选择器

ios - 键盘破坏了 UICollectionView 的单元格

Swift Collection View 选择

ios - 当应用程序后台锁定屏幕未完全动画化时

ios - 如何在 ios 中单击按钮时将 WKInterfaceTimer 值存储在变量中?

iphone - <Google> 在调用 loadRequest 之前必须设置 GADBannerView 的 rootViewController 属性 :