Возникли проблемы с переопределением свойств uitableviewcell

У меня есть собственный TableViewCell, который я создал. Каждая ячейка имеет две метки UI, которые я хочу иметь разного цвета. Кроме того, у меня есть один раздел моего tableView, в котором я хочу иметь другой цвет фона и цвет текста.

Прямо сейчас я не могу изменить цвет текста, используя мой UILabel.textColor; хотя я заставил его работать с cell.textLabel.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;
}

person Miriam H.    schedule 12.05.2011    source источник


Ответы (1)


Вам нужно будет изменить цвет фона ячейки в - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Здесь вы можете выполнить настройку непосредственно перед тем, как таблица отобразит ячейку.

person Christopher Moore    schedule 12.05.2011