2017-09-23 11 views
0

は、私はそれは私のカスタムUITableCellクラススイフト単一のTableViewCellの色を設定する方法は?

内部メソッドです。しかし、あなたがGIF他のセルに見ることができるようにも影響を受けている

if(indexPath.row == colorIndex){ 
    cell.textLabel?.textColor = UIColor.red 
} 

この

func setup(item: MPMediaItem){ 
    self.textLabel?.text = item.value(forProperty: MPMediaItemPropertyTitle) as? String 
    if(currentPlayingItem == item){ 
     self.textLabel?.textColor = UIColor.red 
    } 
} 

を試してみました。どのように私はこれを避けることができますので、インデックスを持つものだけ、3つの色になると言うでしょうか?あなたが着色するための具体的なデータが必要な場合、このcolor attributeがどうあるべき

if(indexPath.row == colorIndex){ 
    cell.textLabel?.textColor = UIColor.red 
} 
else { 
    cell.textLabel?.textColor = UIColor.black 
} 

答えて

2

データそのものに

struct Item { 
    ... // other members 
    var preferredColor:UIColor? // I'd use hex instead so you could transform your hex code to UIColor 
} 

var items = [Item]() // array 

// cellForRowAt 
let dataAtCell = items[indexPath.row] 

cell.textLabel?.textColor = dataAtCell.preferredColor ?? .black // if you don't have a color for that cell then the default color is black 
1

:具体的に黒(または任意の非赤色がある)非赤の行を設定し

enter image description here

関連する問題