Reputation: 274423
My app's theme color is a greenish color, so everything has to be green, like this table view cell:
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:
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?
Upvotes: 2
Views: 2477
Reputation: 169
The method signature of this function has changed slightly in subsequent versions of swift.
in Swift 4+ use
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.red // Your color here!
}
Upvotes: 1
Reputation: 728
Don't know why apple don't put these things in default. Here's the code that change whole UITableViewCell background color:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellView = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath)
//Setup selectedbackgroundView which change color includes editingAccessoy background color
let selectedBackground = UIView()
selectedBackground.frame = cellView.frame
selectedBackground.backgroundColor = SCColor.selectedColor
cellView.selectedBackgroundView = selectedBackground
//Setup backgroundView which change color includes asscessory background color
let cellBackground = UIView()
cellBackground.frame = cellView.frame
cellBackground.backgroundColor = cellData.isSelected ? SCColor.selectedColor : UIColor.white
cellView.backgroundView = cellBackground
}
Upvotes: 0
Reputation: 2459
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:
Upvotes: 10
Reputation: 35783
This is how I have updated my buttons for Swift 2.2
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{
let deleteBtn = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
print("Delete pressed!")
})
deleteBtn.backgroundColor = UIColor.blueColor()//Change color here
let editBtn = UITableViewRowAction(style: .Default, title: "Edit", handler: { (action, indexPath) in
print("Edit pressed!")
})
editBtn.backgroundColor = UIColor.redColor()//change color here
return [deleteBtn, editBtn]
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}
Upvotes: 1
Reputation: 35402
The editingAccessoryView background color can be modified like this:
cell.contentView.superview.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
If the change of contentView.superview
background color dont work you can try this:
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
}
Upvotes: 4