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];
}
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.
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.
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