根据文本单元格的值更改文本单元格的颜色

时间:2013-12-24 16:35:36

标签: ios objective-c uitableview

我想更改UITableView中单元格文本的颜色。

当我尝试使用if进行条件格式化时,它不起作用。

以下是我正在尝试的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return nomearray.count;
}

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{

cell.textLabel.textAlignment = UITextAlignmentRight;


if(indexPath.row % 2 == 0) {

    cell.backgroundColor = [UIColor colorWithRed:(240/255.0) green:(240/255.0) blue:(240/255.0) alpha:1];}

else {
    cell.backgroundColor = [UIColor whiteColor];}

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


if (risco == 1) {[cell.textLabel setTextColor: [UIColor colorWithRed:(60/255.0) green:(169/255.0) blue:(133/255.0) alpha:1]];} else {

    if (risco == 2) {[cell.textLabel setTextColor: [UIColor colorWithRed:(255/255.0) green:(200/255.0) blue:(0/255.0) alpha:1]];} else {

        if (risco == 3) {[cell.textLabel setTextColor: [UIColor colorWithRed:(150/255.0) green:(50/255.0) blue:(50/255.0) alpha:1]];}}}



 cell.textLabel.text = [nomearray objectAtIndex:indexPath.row];

return cell;

}

1 个答案:

答案 0 :(得分:0)

我认为问题是你的条件不满意因为整数值设置不正确。所以只需设置risco整数的属性然后检查,你试试吗?

相关问题