2014-11-10 9 views
1

동적으로 업데이트하는 UILabels가 포함 된 셀이 포함 된 UICollectionView가 있습니다. 셀을 선택하면 셀의 배경색이 변경되지만 레이블의 텍스트 색도 변경됩니다. 현재 다음 코드를 사용하고 있습니다.Swift의 UICollectionView에서 텍스트 색상 변경

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    cell = collectionView.dequeueReusableCellWithReuseIdentifier("targetCell", forIndexPath: indexPath) as UICollectionViewCell 
    var label : UILabel = cell.viewWithTag(100) as UILabel 
    label.textColor = UIColor.whiteColor() 
} 

그러나 셀을 선택할 때 텍스트 색상이 새 색상으로 업데이트되지 않습니다. 어떤 아이디어?

답변

3

재사용 가능한 셀을 dequeuing하고 업데이트하기 때문에.

기본적으로 dequeueReusableCellWithReuseIdentifier:forIndexPath:collectionView:cellForItemAtIndexPath:에만 사용됩니다.

cellForItemAtIndexPath을 사용하면 선택한 셀을 가져올 수 있습니다.

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    if let cell = collectionView.cellForItemAtIndexPath(indexPath) { 
     var label = cell.viewWithTag(100) as? UILabel 
     label?.textColor = UIColor.whiteColor() 
    } 
}