2016-08-24 81 views
0

我一直在尝试更改UICollectionView中自定义单元格内的UILabel的文本颜色。目前我使用下面的代码,让我改变Cell的背景颜色,但我只需要改变文字颜色:更改自定义UICollectionViewCell中UILabel的文本颜色

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

回答

2

如果它是一个自定义单元格,您将需要一个标签添加到您的自定义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) 

} 
+0

非常感谢。这正是我需要的:) – Molly

+0

不客气。如果这有帮助,请选择它作为答案和upvote。 – Nate4436271

0
theLableYouWantToChangeColor.textColor = UIColor.redColor() 

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

+0

究竟这是我做了什么: 类SliderCustomCollectionViewCell:UICollectionViewCell { @IBOutlet VAR sliderCellLabel:的UILabel! } – Molly

+0

现在如何让它在另一个类的函数中可见。具体在didSelect和deSelect方法?最后改变文字的颜色......? – Molly

+0

在cellforItems ....上创建单元格时使用它。我不明白你在做什么“现在如何让它在另一个类函数中可见” – Sofeda

0

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

相关问题