如何隐藏集合视图单元格内的uiview

Posted

技术标签:

【中文标题】如何隐藏集合视图单元格内的uiview【英文标题】:How to hide the uiview inside the collection view cell 【发布时间】:2016-08-24 10:30:15 【问题描述】:

我有一个colelctionViewCell 一个标签和一个UIView。我所做的是,在我的自定义集合视图单元格中,我保留了一种方法。那就是:如果用户按下集合视图单元格。那么只有那个 UIView 的颜色会改变,否则它将是其他颜色。

但我最初需要的是隐藏我的view。对于第一个自定义单元格,只有我的 uiview 应该显示。怎么办??

@IBOutlet weak var ProductCatg: UILabel!                
@IBOutlet weak var highLightBar: UIView!

    override var selected: Bool 
        didSet                                                
            if selected 
                highLightBar.backgroundColor = UIColor.yellowColor()
             else 
                highLightBar.backgroundColor = UIColor.clearColor()
                            
        
    

【问题讨论】:

【参考方案1】:

如果您只想设置所选Cell 的颜色,那么您可以像这样使用selectedBackgroundViewCollectionViewCell

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KCollectionViewCellIdentifier, forIndexPath: indexPath) as! collectionViewCell
    let selectedview = UIView(frame: cell.contentView.bounds)
    selectedview.backgroundColor = UIColor.yellowColor()
    cell.selectedBackgroundView = selectedview
    return cell

注意:无需添加您当前使用的selected 代码。

【讨论】:

以上是关于如何隐藏集合视图单元格内的uiview的主要内容,如果未能解决你的问题,请参考以下文章