user3230481
user3230481

Reputation: 77

How to set different text colours on uitableviews

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

Answers (2)

Muddu Patil
Muddu Patil

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

Wain
Wain

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

Related Questions