我如何 select 并像 UITableViewCell 一样突出显示 UICollectionView?

How do I select and highlight a UICollectionView like a UITableViewCell?

我有一个简单的难题。我的应用程序中有一个 UICollectionViewController,它显示已保存的测验。我有正确的加载,但我希望它移动检测其中之一的点击,然后执行逻辑将其加载到主 VC。我希望选择类似于 UITableViewCell,它突出显示它一秒钟然后取消突出显示。如果有帮助,我正在使用子类化的 UICollectionViewCell。

UICollectionViewCell 有一个 属性

@property(nonatomic, getter=isHighlighted) BOOL highlighted;

您可以使用此 属性 在 cellForItemAtIndexPath:

中选择 UICollectionViewCell

与 UITableView 不同,在 UICollectionView 中没有为此定义特定的方法。您需要通过即时更改特定单元格的背景颜色或背景图像来以编程方式实现它。 你可以参考Apple文档。https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/ 希望对你有帮助。

*****已编辑***** 你也可以参考这个答案.. Why UICollectionView's UICollectionViewCell is not highlighting on user touch? 快乐编码.. :)

为了突出显示,您可以像这样子类化您的单元格:

import UIKit

class CustomCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let backgroundView = UIView(frame: CGRectZero)

        backgroundView.backgroundColor = UIColor.blueColor()

        self.selectedBackgroundView = backgroundView
    }
}

来自 the docs UICollectionViewCell:

You can use this view to give the cell a custom appearance when it is selected. When the cell is selected, this view is layered above the backgroundView and behind the contentView.