KerrM
KerrM

Reputation: 5240

selected UITableViewCell background color changes upon scroll

I've got these cells I have set a custom background colour to. The background colour works fine when I select the cell, however, when I scroll down and back up, two things can happen:

  1. If not many cells are selected, the cells that went out of view sometimes come back with the default blue colour when selected.

  2. If most or all of the cells are selected, the cells that went out come back with one of the colours that is on the cells that were there beforehand - ie. I select all the cells, scroll down and back up and the cells at the top have the same colour as the cells at the bottom (or at least some of them - others retain their own colour).

Here is the code I have that produces this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *row = [tableView cellForRowAtIndexPath:indexPath];
    UIView *backview = [[UIView alloc] initWithFrame:row.frame];
    backview.backgroundColor = [colours objectAtIndex:indexPath.row];
    row.selectedBackgroundView = backview;
}

That's where the selected method for the cells changes the colour. The cells are created here:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"eventTypeID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSString *sEventType = [[self.eventTypes valueForKeyPath:@"name.text"] objectAtIndex:indexPath.row];
    cell.textLabel.text = sEventType;
    return cell;
}

And the colours for each cell are set here:

- (void)loadView {
    colours = [[NSMutableArray alloc] init];
    CGFloat red[] = {0.84,0.86,0.7,0.46,0.56,0.44,0.95,0.91,0.91,0.76,0.06,0.8,0.73,0.0,0.0,0.01,0.18,0.23,0.57,0.18};
    CGFloat green[] = {0.12,0.01,0.07,0.17,0.32,0.18,0.49,0.49,0.78,0.61,0.48,0.85,0.85,0.28,0.38,0.53,0.23,0.36,0.32,0.24};
    CGFloat blue[] = {0.34,0.5,0.2,0.53,0.55,0.31,0.18,0.18,0.12,0.27,0.14,0.1,0.49,0.1,0.37,0.49,0.4,0.41,0.55,0.40};
    for (int i = 0; i < 20; i++) {
        [colours addObject: [UIColor colorWithRed:red[i] green:green[i] blue:blue[i] alpha:1.0]];
    }
//Get data from server and parse it
...
}

Now, I have only just started programming the iPhone but my guess (and this is a wild one btw) is that the cells are getting re-created in cellForRowAtIndexPath and although some of the properties are getting saved (like the title...) the custom background isn't.

Has anyone come across this behaviour before? If so, how did you solve it?

EDIT: Even weirder behaviour: Sometimes, if you scroll back down and up, the cell that had gone to the "default" selected background colour goes back to it's custom one. The behaviour seems to be random.

Upvotes: 3

Views: 3034

Answers (1)

Tark
Tark

Reputation: 5173

Cell background colours are set in many places, to ensure that the background displayed is the one you want you need to use:

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

see this question for more details. If you require custom selection colours, then you should subclass UITableViewCell and override - (void)setSelected:(BOOL)selected and - (void)setHighlighted:(BOOL)highlighted

Upvotes: 7

Related Questions