繁体   English   中英

在Custom UICollectionViewCell中更改UILabel的文本颜色

[英]Change text color of UILabel inside Custom UICollectionViewCell

我一直在尝试在UICollectionViewUICollectionView自定义单元格内的UILabel的文本颜色。 目前我正在使用以下代码,它允许我更改Cell背景颜色,但我只需要更改文本颜色:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
    {

    //Change background color of selected Cell

    let selectedCell:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!

    selectedCell.contentView.backgroundColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)

    }


func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
    {

    //Set background color of selected Cell to Clear Color 

    let cellToDeselect:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!

    cellToDeselect.contentView.backgroundColor = UIColor.clearColor()

    }

我见过很少的应用程序,其中一条发线类型的东西在选定的单元格下继续移动。 任何人都知道如何实现它?

TIA

如果是自定义单元格,则需要向自定义UICollectionViewCell添加标签

import UIKit

class CustomCell: UICollectionViewCell {

    let label: UILabel! = nil

}

然后,在selectItemAtIndexPath中:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let selectedCell: CustomCell = collectionView.cellForItemAtIndexPath(indexPath) as! CustomCell
    selectedCell.label.textColor = UIColor(red: 102/256, green: 255/256, blue: 255/256, alpha: 0.66)

}
theLableYouWantToChangeColor.textColor = UIColor.redColor()

正如你所说的自定义UICollectionViewCell ,你必须创建一个自定义的UICollectionViewCell并在里面添加一个UILabel

您应该有单元格引用并使该标签属性可以从其他类访问并访问并更改它或将颜色对象传递给单元格并仅在那里更改它。 其他检查:参考不应该是零,如果IBOutlet那么它应该连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM