2014-09-25 335 views
13

我想弄清楚如何以編程方式(不是在故事板)設置UITableViewCellAccessoryType.Checkmark的顏色。SWIFT - UITableViewCellAccessoryType更改複選標記顏色

我覺得有點愚蠢問如何做到這一點一樣簡單,但我找不到蘋果文檔中的答案。任何幫助都會很棒。謝謝。

我設置的附件類型是這樣的:(正常工作)

 cell.accessoryType = .Checkmark 

編輯:如果您想更改用於對號的形象,這爲我工作。但是POB的答案是我最終用來改變顏色的。

let checkImage = UIImage(named: "checkmark.png") 
    let checkmark = UIImageView(image: checkImage) 

    cell.accessoryView = checkmark 
+1

參見[2回答這個問題以前(http://stackoverflow.com/questions/7641228/change-color-on-checkmark-in-uitableview)(Objective C)。 – 2014-09-25 18:43:48

+0

@POB我看...感謝您的鏈接。將更新與工作代碼的帖子:) – 2014-09-25 18:49:25

+1

cell.accessoryView =自定義選中標記圖像 - >仍然適用於swift 2.2 – fatihyildizhan 2016-01-23 17:50:22

回答

20

下面的代碼應該工作:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

    //Change cell's tint color 
    cell.tintColor = UIColor.redColor() 

    //Set UITableViewCellAccessoryType.Checkmark here if necessary 
    cell.accessoryType = .Checkmark 

    /* ... */ 

    return cell 
} 
+0

這工作更好然後我anwser :) – 2014-09-25 18:58:56

4

這裏是代碼雨燕3.0

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

    cell.tintColor = UIColor.red 
    cell.accessoryType = .checkmark 

    return cell 
}