2016-08-24 5 views
0

UILabelのテキストセルをカスタムセル内のUICollectionViewに変更しようとしています。私は事のヘアラインの種類を選択下に移動し続けるているいくつかのアプリケーションを見てきましたカスタム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() 

    } 

:現在、私は私がCellの背景色を変更することができますが、私は唯一のテキストの色を変更する必要があり、次のコードを使用しています細胞。誰も、それを実装する方法を知っていますか?

TIA

答えて

2

あなたがselectItemAtIndexPathで、その後

import UIKit 

class CustomCell: UICollectionViewCell { 

    let label: UILabel! = nil 

} 

UICollectionViewCell

カスタムにラベルを追加する必要がありますそのカスタムセルの場合:

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

あなたはセル参照を持っており、そのラベルのプロパティにアクセスできるようにすべきです他のクラスからアクセスして変更するか、またはカラーオブジェクトをセルに渡してそこに変更するだけです。 他のチェック:参照はnilであってはなりません.IBOutletが接続されている場合は参照してください。

関連する問題