2017-09-23 3 views
0

나는 그것은 내 사용자 지정 UITableCell 클래스Swift 단일 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이 착색된다고 가정 해 봅시다.

enter image description here

답변

2

특히 검은 색으로 비 레드 행을 설정 (또는 비 붉은 색 무엇이든) : 사용자가 특정 데이터를 착색 할 경우

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

하는 것은 다음이 color attribute이 있어야한다 데이터 그 자체.

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 
관련 문제