Reputation: 77
I have a dynamic UITableview
and I want to set different text colours for each cell.
What code would i need to make the text colours be red, yellow, green, blue?
Upvotes: 0
Views: 43
Reputation: 721
Here the following code will help you
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
if(indexPath.row % 4 == 0)
cell.textLabel.textColor =[UIColor redColor];
else if(indexPath.row % 4 == 1)
cell.textLabel.textColor =[UIColor blueColor];
else if (indexPath.row % 4 == 2)
cell.textLabel.textColor =[UIColor yellowColor];
else if (indexPath.row % 4 == 2)
cell.textLabel.textColor =[UIColor greenColor];
}
Upvotes: 1
Reputation: 119031
You need some code to decide how to choose the colour. This could be colours in an array and a mod (%
) of the index path row to choose the index in the array. You then want to use either the textColor
or the attributedText
property of the label to set (attributed) text including the colour.
Upvotes: 0