重写uitableviewcell属性时遇到问题

重写uitableviewcell属性时遇到问题,uitableview,ios4,sections,Uitableview,Ios4,Sections,我已经创建了一个自定义TableViewCell。每个单元格有两个UILabel,我希望它们是不同的颜色。另外,我的tableView中有一个部分需要有不同的背景颜色和文本颜色 现在,我无法使用UILabel.textColor更改文本颜色;虽然我确实让它与cell.textlab.textColor一起工作。另外,cell.background颜色不起作用 如果我在底部的switch语句中这样做,我就能够使单元格属性的更改生效,但是单元格属性不能正确地显示,因此我上下滚动时,随机单元格将变成红

我已经创建了一个自定义TableViewCell。每个单元格有两个UILabel,我希望它们是不同的颜色。另外,我的tableView中有一个部分需要有不同的背景颜色和文本颜色

现在,我无法使用UILabel.textColor更改文本颜色;虽然我确实让它与cell.textlab.textColor一起工作。另外,cell.background颜色不起作用

如果我在底部的switch语句中这样做,我就能够使单元格属性的更改生效,但是单元格属性不能正确地显示,因此我上下滚动时,随机单元格将变成红色背景上的白色文本

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

    UILabel* itemLabel;
    UILabel* amountLabel;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    itemLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 35.0)] autorelease];
    itemLabel.tag = ITEMLABEL_TAG;
    itemLabel.font = [UIFont systemFontOfSize:14.0];
    itemLabel.textAlignment = UITextAlignmentLeft;
    itemLabel.autoresizingMask = UIViewAutoresizingNone;
    itemLabel.backgroundColor = [UIColor clearColor];
    itemLabel.textColor = [UIColor blueColor]; //This doesn't work

    if (indexPath.section == 4)
    {
        cell.textLabel.textColor = [UIColor whiteColor]; //This works
        cell.backgroundColor = [UIColor redColor]; //This doesn't work
    }
    [cell.contentView addSubview:itemLabel];

    cell.backgroundColor = [UIColor whiteColor];

    amountLabel = [[[UILabel alloc]initWithFrame:CGRectMake(225.0, 0.0, 55.0, 35.0)] autorelease];
    amountLabel.tag = AMOUNTLABEL_TAG;
    amountLabel.font = [UIFont systemFontOfSize:14.0];
    amountLabel.textAlignment = UITextAlignmentLeft;
    amountLabel.textColor = [UIColor blackColor]; //This doesn't work
    amountLabel.backgroundColor = [UIColor clearColor];
    amountLabel.autoresizingMask = UIViewAutoresizingNone;
    [cell.contentView addSubview:amountLabel];


    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = [monthlyExpenses objectAtIndex:indexPath.row];
            break;
        case 1:
            cell.textLabel.text = [oneTimeFees objectAtIndex:indexPath.row];
            break;
        case 2:
            cell.textLabel.text = [renters objectAtIndex:indexPath.row];
            break;
        case 3:
            cell.textLabel.text = [travelExpenses objectAtIndex:indexPath.row];
            break;
        case 4:
            cell.textLabel.text = @"Generate Report";
            break;
    }

return cell;
}

您需要更改
-(void)tableView:(UITableView*)tableView将显示cell:(UITableViewCell*)单元格中单元格的背景色,以用于rowatindexpath:(nsindepath*)indepath

在这里,您可以在表格显示单元格之前进行自定义