I am making a simple app with a tableview that displays results of a test. The results are coming from a simple array. In the array there are just numbers, test scores between 0 and 100.
I’m trying to get the UITableView row to change the colour depending on the results. Above or equal to 75 will display a green background, >= 50 && < 75 will be yellow, > 50 will be red.
This is what I have so far. My understanding is quite basic.
- (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];
}
// Configure the cell...
// Set up the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [scoresArray objectAtIndex:row];
// THIS IS WHERE I NEED HELP TO GET THE VALUE FROM THE ARRAY
// INTO ????
if (???? >=75) {
cell.contentView.backgroundColor = [UIColor greenColor];
}
if (???? >=50 && ???? <75) {
cell.contentView.backgroundColor = [UIColor yellowColor];
}
if (???? >=0 && ???? <50) {
cell.contentView.backgroundColor = [UIColor redColor];
}
else {
cell.contentView.backgroundColor = [UIColor whiteColor];
}
return cell;
}
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView willDisplayCell:
(UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor = cell.contentView.backgroundColor;
}
If I just put cell.contentView.backgroundColor = [UIColor greenColor];, for example, they all go green.
Assuming that the value is displaying correctly in the textLabel, you can use this: