简体   繁体   English

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

[英]Change text color of UILabel inside Custom UICollectionViewCell

I have been trying to change the Text Color of a UILabel inside a Custom cell in UICollectionView . 我一直在尝试在UICollectionViewUICollectionView自定义单元格内的UILabel的文本颜色。 Currently I am using following code which allows me to change background color of the Cell but I only need to change the text color: 目前我正在使用以下代码,它允许我更改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()

    }

I have seen few apps in which a hair line kind of thing keeps moving under selected cell. 我见过很少的应用程序,其中一条发线类型的东西在选定的单元格下继续移动。 Anyone knows how to implement that as well? 任何人都知道如何实现它?

TIA TIA

If its a custom Cell you will need to add a label to your custom UICollectionViewCell 如果是自定义单元格,则需要向自定义UICollectionViewCell添加标签

import UIKit

class CustomCell: UICollectionViewCell {

    let label: UILabel! = nil

}

Then, in selectItemAtIndexPath: 然后,在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

You should have cell reference and make that label property accessible from other class and access and change that Or pass the color object to cell and change it there only. 您应该有单元格引用并使该标签属性可以从其他类访问并访问并更改它或将颜色对象传递给单元格并仅在那里更改它。 Other checks: Reference should not be nil, If IBOutlet then it should be connected. 其他检查:参考不应该是零,如果IBOutlet那么它应该连接。

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

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