Swift - 如何更改accessoryType(disclosureIndicator)的颜色?

时间:2017-07-05 13:05:41

标签: ios swift tableview cell

我对accessoryType个细胞有疑问。我正在使用disclosureIndicator作为accessoryType的单元格,我想改变它的颜色,但我不能。 有谁知道这是一个错误还是Apple迫使我使用灰色? 实际上我可以改变其他accessoryType的颜色。

我的代码如下所示:

let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! customCell
cell.tintColor = UIColor.red
cell.accessoryType = .disclosureIndicator

我的箭仍然是灰色的。但如果我使用勾选标记accessoryType,它会变为红色。

有没有办法解决这个问题,还是我必须使用彩色图像?

5 个答案:

答案 0 :(得分:8)

你可以做这样的事情

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

    let image = UIImage(named: "Arrow.png")
    let checkmark  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!));
    checkmark.image = image
    cell.accessoryView = checkmark

    let object = objects[indexPath.row] as! NSDate
    cell.textLabel!.text = object.description
    return cell
}

示例箭头图片

enter image description here enter image description here

输出

enter image description here

答案 1 :(得分:3)

使用SF符号

let image = UIImage(systemName: "chevron.right")
let accessory  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!))
accessory.image = image

// set the color here
accessory.tintColor = UIColor.white
cell.accessoryView = accessory

答案 2 :(得分:2)

Bellow Code是 Swift 3.0 代码,并会根据tintColor更改 accessoryType颜色。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "SOME TITLE GOES HERE"

    cell.accessoryType = .disclosureIndicator

    cell.tintColor = UIColor.blue


    let image = UIImage(named:"arrow1")?.withRenderingMode(.alwaysTemplate)

    let checkmark  = UIImageView(frame:CGRect(x:0, y:0, width:(image?.size.width)!, height:(image?.size.height)!));
    checkmark.image = image
    cell.accessoryView = checkmark

    return cell
}

enter image description here

答案 3 :(得分:2)

已针对带有附件的图片的Swift 4.2更新

    cell.accessoryType = .disclosureIndicator
    cell.tintColor = .black
    let image = UIImage(named:"disclosureArrow")?.withRenderingMode(.alwaysTemplate)
    if let width = image?.size.width, let height = image?.size.height {
        let disclosureImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
        disclosureImageView.image = image
        cell.accessoryView = disclosureImageView
    }

您可以使用的图像:

1x 2x 3x

可能是这样的:

example

答案 4 :(得分:0)

Swift 5扩展样式;)

extension UITableViewCell {
    func setupDisclosureIndicator() {
        accessoryType = .disclosureIndicator
        let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 7, height: 12))
        imgView.contentMode = .scaleAspectFit
        imgView.image = UIImage(named: "your_icon_name")
        accessoryView = imgView
    }
}