2009-10-01 30 views
10

Khi tôi đặt UITableViewCells backgroundColor thành màu bán trong suốt, có vẻ tốt, nhưng màu không bao phủ toàn bộ ô.Nền trong suốt UITableViewCell (bao gồm imageView/accessoryView)

Khu vực xung quanh imageViewaccessoryView đang mọc lên như [UIColor clearColor] ...

alt text

Tôi đã cố gắng thiết lập một cách rõ ràng cell.accessoryView.backgroundColorcell.imageView.backgroundColor là màu giống như các tế bào của backgroundColor, nhưng nó không hoạt động. Nó đặt một hộp nhỏ xung quanh biểu tượng, nhưng không mở rộng để lấp đầy cạnh trái. Cạnh phải có vẻ không bị ảnh hưởng bởi điều này.

Làm cách nào để khắc phục sự cố này?

EDIT: Đây là mã ô trong bảng liệu:

- (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; 
} 

Trả lời

16

Ben và tôi đã tìm ra điều này ngày hôm nay, đây là tóm tắt cho nhóm trong trường hợp điều này bắt bất kỳ ai khác.

Bạn phải đặt nền ô và cell.textLabel.backgroundColor mỗi lần cellForRowAtIndexPath được gọi, không chỉ trong giai đoạn alloc/init (ví dụ: nếu tableView có thiếu bộ nhớ cache dequeue).

Vì vậy, mã trở này:

- (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; 
} 
+1

thực sự tôi phải làm thêm 1 điều nữa. Cell.textLabel.backgroundColor cần được thiết lập * SAU * màu nền của ô. Tôi KHÔNG có ý tưởng tại sao tôi cần phải làm điều này, nhưng bây giờ nó hoạt động. Cảm ơn Mike! –

2

Các bạn nhìn vào cách thiết lập màu nền của các tế bào của contentView và giữ các tế bào, phụ kiện, và xem hình ảnh minh bạch?

Ngoài ra, this article có thể giúp hiển thị cho bạn một số lựa chọn thay thế.

+0

Tôi đã sử dụng mà bài để tìm cảm hứng, nhưng tiếc là ông không sử dụng các tế bào trong suốt. Đặt màu nền của contentView có hiệu ứng rất lạ. Tôi cũng đã cố gắng thiết lập contentView.opaque = NO, nhưng vẫn trông khủng khiếp. –