4

I have a UITableView and I have set my cell background color to RGB 244, 240, 246. I've done this by setting the background color on the table, table cell, and the content view in the table cell.

However, the accessory (in this case the checkmark) has a black background instead.

UIAccessoryView has black background

When I enable editing on the table, the delete circle on the left side also has a black background.

Editing enabled has more black background

I cannot seem to change this background color.

I've tried doing so with the following code:

cell.editingAccessoryView = [[UIView alloc] init];
cell.editingAccessoryView.backgroundColor = [UIColor colorWithRed:244/255 green:240/255 blue:246/255 alpha:1.0];

but it has no effect.

I've tried looking for a setting within the storyboard but nothing seems to make any difference.

I did notice that if I change the table cell and content view background color to "default" the whole cell background becomes black (even though the table background color is still my custom color).

I've gone through the iOS7 Transition guide and I didn't see anything related to the UIAccessoryView. I've also searched through stackoverflow but I wasn't able to find anything matching the issue I'm having.

How can I fix this?

kinadian
  • 238
  • 1
  • 3
  • 11
  • 1
    I think you need to change the background color using a [different method in iOS 7][1] [1]: http://stackoverflow.com/questions/18878258/uitableviewcell-show-white-background-and-cannot-be-modified-on-ios7 – RyanR Oct 16 '13 at 14:54
  • I'm able to change the cell background color to anything I want and it works just fine. I didn't do it in code, but I did it via the content view within the table cell as one of the answers to that question suggested. What I'm not able to change is the accessory view background color. – kinadian Oct 16 '13 at 15:05
  • The accessory view is not inside the contentView of the cell, that's why I was recommending changing the background of the entire cell. – RyanR Oct 16 '13 at 15:12
  • I was setting the cell background to my custom color in the storyboard as well (not just the content view in the cell). However, I just added it to my cellForRowAtIndexPath method (cell.backgroundColor) and I still get the black background in the accessory view. – kinadian Oct 16 '13 at 15:15
  • I was having basically the same problem (but my accessory is a custom view, not a system type), and the default white background was showing up on iOS7. The first comment by RyanR worked perfectly for me, ie, going to the other SO question and looking at the accepted answer about tableView:willDisplayCell:forRowAtIndexPath: . That solved my issue and may solve yours. – chadbag Oct 28 '13 at 22:22
  • Well, I've finally figured it out. My problem wasn't how I was changing the accessory background colour but how I was creating the colour itself. I was able to fix it by adding .0f to all my 255's (i.e. colorWithRed:244/255.0f). My best guess is that without the .0f it was treating the numbers as ints and truncating all the values to 0 (which would give me black). I'm not sure how to finish this question. I'm assuming it would be tacky to submit an answer myself and accept it as the correct one. – kinadian Apr 01 '14 at 15:49

5 Answers5

2

You'll need to set the backgroundView property of the UITableViewCell, you can't simply change the editingAccessoryView, as you've probably seen. Instead do something like:

UIView *aBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
aBackgroundView.backgroundColor = [UIColor yourColor];
yourCell.backgroundView = aBackgroundView;

Or in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Use:

cell.contentView.superview.backgroundColor = [UIColor redColor];
Josiah
  • 4,663
  • 2
  • 31
  • 49
  • Unfortunately neither of your suggestions corrected it for me. Both of them result in the same black background on the accessory as in my screenshots. – kinadian Oct 16 '13 at 15:20
  • @kinadian: Curious. I don't recall Apple changing the spec in iOS 7, so it makes me wonder if this is a platform bug you're dealing with... – Josiah Oct 19 '13 at 16:07
2

In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

I hope this help you

0

For table view cell, there is a property called backgroundView. You only need to change the backgroundcolor for the backgroundView. Thats it.

 cell.backgroundView.backgroundColor = [UIColor yourcolor];
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
Yllow
  • 326
  • 4
  • 7
0

For iOS 7 place this in customised table cell code

- (void)willTransitionToState:(UITableViewCellStateMask)state
{
    [super willTransitionToState:state];
    if ((state & UITableViewCellStateShowingEditControlMask) == UITableViewCellStateShowingEditControlMask)
    {
        for (UIView *subview in self.subviews)
        {
            if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellScrollView"])
            {
                subview.backgroundColor  = [UIColor colorWithRed:0.69 green:0.769 blue:0.871 alpha:1];
            }
        }
    }

}

swainwri
  • 66
  • 7
0

The problem was caused by the colour object. The correct line is:

cell.editingAccessoryView.backgroundColor = [UIColor colorWithRed:244/255.0f green:240/255.0f blue:246/255.0f alpha:1.0];

My best guess is that without the .0f it was treating the numbers as ints and truncating all the values to 0 (which would give me black).

kinadian
  • 238
  • 1
  • 3
  • 11