0

作成したカスタム TableViewCell があります。各セルには、異なる色にしたい 2 つの UIlabel があります。また、別の背景色とテキスト色を使用したい tableView のセクションが 1 つあります。

現在、UILabel.textColor を使用してテキストの色を変更することはできません。cell.textLabel.textColor で動作するようになりましたが。また、 cell.background color が機能していません。

一番下の 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;
}
4

1 に答える 1

0

セルの背景色を変更する必要があります- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

ここで、表にセルが表示される直前にカスタマイズを行うことができます。

于 2011-05-12T13:37:52.997 に答える