颜色编码表查看单元格

时间:2014-02-24 21:51:53

标签: ios objective-c uitableview

我试图根据属于partySize的每个成员的属性mutableArray对我的tableview进行颜色编码,该属性是我的tableview的数据源。我希望在willDisplayCell和NOT cellForRowAtIndexPath中执行此操作时,如果我有代码,没有任何反应,所有单元格都是白色的。中间列是列出partySize数据的位置。

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

    cell = [tableView cellForRowAtIndexPath:indexPath];
    QueueMember *member = (self.queue)[indexPath.row];
    switch (member.partySize) {
        case 0:
        case 1:
        case 2:
            cell.backgroundColor = [UIColor redColor];
            break;
        case 3:
        case 4:
            cell.backgroundColor = [UIColor blueColor];
        case 5:
        case 6:
            cell.backgroundColor = [UIColor orangeColor];
        case 7:
        case 8:
            cell.backgroundColor = [UIColor greenColor];
        case 9:
        case 10:
            cell.backgroundColor = [UIColor yellowColor];
        default:
            cell.backgroundColor = [UIColor purpleColor];
            break;
    }
}

我已经查看了堆栈溢出,并且知道有很多方法可以在'cellForRowAtIndexPath中执行此操作,但我在此处执行此操作,因为这是'uitableviewcell'的Apple开发人员类引用。

1 个答案:

答案 0 :(得分:0)

1.将break添加到您的开关

switch (member.partySize) {
        case 0:
        case 1:
        case 2:
            cell.backgroundColor = [UIColor redColor];
            break;
        case 3:
        case 4:
            cell.backgroundColor = [UIColor blueColor];
            break;
        case 5:
        case 6:
            cell.backgroundColor = [UIColor orangeColor];
            break;
        case 7:
        case 8:
            cell.backgroundColor = [UIColor greenColor];
            break;
        case 9:
        case 10:
            cell.backgroundColor = [UIColor yellowColor];
            break;
        default:
            cell.backgroundColor = [UIColor purpleColor];
            break;
    }

2.尝试将cell.backgroundColor更改为cell.contentView.backgroundColor