如何在UITableView上设置不同的文本颜色

如何在UITableView上设置不同的文本颜色,uitableview,text,colors,Uitableview,Text,Colors,我有一个动态UITableview,我想为每个单元格设置不同的文本颜色 我需要什么代码使文本颜色为红色、黄色、绿色、蓝色 您需要一些代码来决定如何选择颜色。这可以是数组中的颜色和索引路径行的mod(%)来选择数组中的索引。然后,您需要使用标签的textColor或attributedText属性来设置(属性化)文本,包括颜色。以下代码将帮助您 - (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*

我有一个动态
UITableview
,我想为每个单元格设置不同的文本颜色


我需要什么代码使文本颜色为红色、黄色、绿色、蓝色

您需要一些代码来决定如何选择颜色。这可以是数组中的颜色和索引路径行的mod(
%
)来选择数组中的索引。然后,您需要使用标签的
textColor
attributedText
属性来设置(属性化)文本,包括颜色。

以下代码将帮助您

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