Color doesn't change in UITableViewCells

Go To StackoverFlow.com

0

I have a UITable that has over 200+ cells. The table works beautifully with data coming in from the network. Now I need to add in a way to change the background color of the label to match with the data (Red if the value decreased and green if the value increased). This seems to work well initially, but after awhile the colors become static even if the values update fine. Below is an example of my code that is in the layoutSubviews method:

Update

I have updated the code to show my my table. Please note that the data gets assigned to the cell perfectly fine. It is the color of the cell that refuses to change after a few minutes no matter what the value is.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UILabel* label = [[UILabel alloc] initWithFrame:self.contentView.bounds];
        label.tag = 1;
        [cell.contentView addSubview:label];
        [label release];
    }
    UILabel *label = (UILabel*)[cell viewForTag:1];
    float value = [label.text floatValue];
    float newValue = [dataSource objectAtIndex:indexPath.row];

    // Get the current value of the cell and compare it with the new value
    if(value < newVal)
    {
        label.backgroundColor = [UIColor greenColor];
    }
    else if(value > newVal)
    {
        label.backgroundColor = [UIColor redColor];
    }
    label.text = [NSString stringWithFormat:@"%d", newValue];
}
2012-04-05 16:56
by Seb
Why is this code in layoutSubviews? That isn't going to be called every time. It should be in cellForRowAtIndexPath - jrturton 2012-04-05 19:32
After reading an answer below I moved the code to it's proper place. Thank you for noticing - Seb 2012-04-05 19:52


0

You are comparing the value from your datasource to the value that was previously displayed in the cell.

This is not going to be a valid comparison. You will have something like 8 or 10 total instances of your cell, which are dequeued and reused. So you will be comparing the datasource of, say, cell 60, with the last value used in this cell, which could have been from row 45. It is meaningless.

You need to store the old value somewhere in your datasource, and make the comparison to that instead.

2012-04-05 21:04
by jrturton
Would creating a dictionary and storing a row index and the old value work? That way when I do the comparison again I can pull the old value from the dictionary based on the row that needs it - Seb 2012-04-05 21:23
Yes, or your datasource could be an array of dictionaries, each with two keys, old and new value - jrturton 2012-04-05 21:32
That seemed to do it. Thank you for your help and going a bit more in-depth with the UITableViewCell. I understood that the cells were reused, what I didn't understand was why the cells that were in view would be reused - Seb 2012-04-05 22:36


0

Setting the background color of the cell won't work because there is another view, its contentView, sitting on top of that. Since iOS handles drawing like a painter's canvas, the contentView obstructs whatever changes you make to the cell's background. You can, however, set the background color on the contentView (it's a UIView, after all), and you should see your green and red colors.

2012-04-05 19:29
by jmstone617
While I am able to see the colors I get the same issue as originally mentioned. After awhile the colors don't seem to update anymore. I tried scrolling to see if that would help, but the same cells always get set to the same color no matter what value it gets set to - Seb 2012-04-05 20:04


0

Well it might be because of deQueue, what you can try is before adding new subviews to your cell run a loop and remove all the sub views from it. Make sure you will do after checking the condition of null cell.

for(UIView *subview in cell.subviews){
  [subview removeFromSuperview];
} 

Might Help

2012-04-05 19:37
by Nilesh
Ads