Search code examples
uitableviewbackgroundbackground-colorios5.1uiappearance

Changing background color of selected UITableViewCells in iOS5


My app displays grouped static UITableViews in different screens. Is there anyway of using the appearance proxies

[UITableView appearance]

or

[UITableViewCell appearance]

to customise the background color for the selected cells? Basically I would need to modify

cell.selectedBackgroundView.backgroundColor

for each cell in my app,but I cannot find the right property to be set in the proxy object.

BTW I have tried also the normal approach (being a group static table):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

}

but it doesn't work. Any suggestion?


Solution

  • You need to provide a view to selectedBackgroundView. There isn't one already there. Try:

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds] ;
    cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor] ;
    

    Also, a better place to put this code (at least the first line) is in -tableView:cellForRowAtIndexPath: when the cell is created. Otherwise you will be creating a new background view every time the cell is displayed.