UITableViewCell прозрачный фон (включая imageView / accessoryView) - PullRequest
10 голосов
/ 01 октября 2009

Когда я устанавливаю UITableViewCells backgroundColor в полупрозрачный цвет, он выглядит хорошо, но цвет не охватывает всю ячейку.

Область вокруг imageView и accessoryView выглядит как [UIColor clearColor] ...

alt text

Я пытался явно установить cell.accessoryView.backgroundColor и cell.imageView.backgroundColor, чтобы они были того же цвета, что и ячейка backgroundColor, но это не работает. Он помещает крошечный прямоугольник вокруг иконки, но не расширяется, чтобы заполнить левый край. Правый край кажется незатронутым этим.

Как я могу это исправить?

РЕДАКТИРОВАТЬ : Вот код необработанной ячейки таблицы:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.opaque = NO;
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4];
        cell.textColor = [UIColor whiteColor];
    }

    cell.imageView.image = [icons objectAtIndex:indexPath.row];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

Ответы [ 2 ]

16 голосов
/ 03 октября 2009

Бен и я выяснили это сегодня, вот сводка для группы на случай, если это кого-нибудь поймает.

Вы должны установить фон ячейки и cell.textLabel.backgroundColor каждый раз, когда вызывается cellForRowAtIndexPath, а не только во время фазы alloc/init (то есть, если tableView имеет промах кэша очереди).

Итак, код становится таким:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.opaque = NO;        
    }

    // All bgColor configuration moves here
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.4];
    cell.textColor = [UIColor whiteColor];
    cell.imageView.image = [icons objectAtIndex:indexPath.row];
    cell.textLabel.text = [items objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
2 голосов
/ 01 октября 2009

Вы смотрели на настройку цвета фона ячейки contentView и на поддержание прозрачности представлений ячейки, аксессуара и изображения?

Кроме того, эта статья может помочь вам показать некоторые альтернативы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...