2014-09-25 345 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 
}