Sweeper Sweeper - 6 months ago 31
Swift Question

How do I set the background color of the editing accessory of a table view?

My app's theme color is a greenish color, so everything has to be green, like this table view cell:

enter image description here

The problem is that when I click the Edit button, a little minus sign pops up on the left of the cell and that isn't green:

enter image description here

I don't understand why this happens. This is my code in

cellForRowAtIndexPath


let cell = UITableViewCell()
cell.textLabel?.text = someStuff
cell.textLabel?.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
cell.contentView.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)

// Look! here I set the color of the editing accessory!
cell.editingAccessoryView?.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
return cell


As you can see, I've set everything to green, including the text label, the background, and even the
editingAccessoryView
! But that thing just isn't green! It stays white as you can see above.

Is there something else I have to set to make that green?

Answer

You have to do it in the tableView:willDisplayCell:forRowAtIndexPath: method. You can it like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel?.text = "MyText"
    return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
}

Edit:

Here is the visual result of the code above: enter image description here

Comments