在自定义 UICollectionViewCell 中更改 UILabel 的文本颜色

Change text color of UILabel inside Custom UICollectionViewCell

我一直在尝试更改 UICollectionView 中自定义单元格内 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

theLableYouWantToChangeColor.textColor = UIColor.redColor()

正如您所说的自定义 UICollectionViewCell,您必须创建一个自定义 UICollectionViewCell 并在其中添加一个 UILabel

如果它是自定义 Cell,您需要向自定义 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)

}

您应该有单元格引用并使该标签 属性 可从其他 class 访问并访问和更改它,或者将颜色对象传递给单元格并仅在该处进行更改。 其他检查:Reference 不应该为 nil,如果是 IBOutlet 那么它应该被连接。