Altering the background color of cell.accessoryView and cell.editingAccessoryView

前端 未结 5 799
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 13:45

Whenever I add an accessoryView to my UITableViewCell, it doesn\'t carry the background color across? I\'m setting a UISwitch as my accessoryView, and the color I have set i

5条回答
  •  粉色の甜心
    2020-12-05 14:15

    Upon reading the documentation (a novel idea), I found the article, "A Closer Look at Table-View Cells". It helped me understand the composition of the cells, and I found my answer...

    cells look like this...

    alt text

    Since the cell.accessoryView is a sister view to cell.contentView I had to ask the cell.contentView for its superview, and then I was able to change the background color for both views at once. Here's what the code looks like...

    // Cell Formatting
    cell.contentView.superview.backgroundColor = [UIColor greenColor];
    

    I know it's really simple, but I'm a newbie and it took me ages to slow down and read the doc. Hopefully, this helps some other folks out there!

提交回复
热议问题