UITableViewCell transparent background (including imageView/accessoryView)
Solution 1
Ben and I figured this out today, here's the summary for the group in case this catches anybody else.
You have to set the cell background and cell.textLabel.backgroundColor
every time cellForRowAtIndexPath
is called, not just during the alloc/init
phase (i.e. if the tableView
has a dequeue cache miss).
So, the code becomes this:
- (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;
}
Solution 2
Have you looked into setting the background color of the cell's contentView
and keeping the cell, accessory, and image views transparent?
Also, this article might help show you some alternatives.
Related videos on Youtube
smirkingman
I'm an iOS & Rails developer working out of Houston, TX. I run [NSScreencast](http://nsscreencast.com), a site that delivers high quality screencasts for Swift developers. Other projects include: Combine Swift video course TonalTherapy GiggleTouch NSDateFormatter.com You can find me on twitter or on my blog.
Updated on June 04, 2022Comments
-
smirkingman almost 2 years
When I set the UITableViewCells
backgroundColor
to a semi-transparent color, it looks good, but the color doesn't cover the entire cell.The area around the
imageView
andaccessoryView
are coming up as[UIColor clearColor]
...I've tried explicitly setting the
cell.accessoryView.backgroundColor
andcell.imageView.backgroundColor
to be the same color as the cell'sbackgroundColor
, but it doesn't work. It puts a tiny box around the icon, but doesn't expand to fill the left edge. The right edge seems unaffected by this.How can I fix this?
EDIT : Here is the raw table cell code:
- (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; }
-
smirkingman over 14 yearsI was using that article for inspiration, but unfortunately he doesn't use transparent cells. Setting the contentView background color had very strange effects. I also tried setting contentView.opaque = NO, but still looked awful.
-
smirkingman over 14 yearsactually I had to do 1 more thing. The cell.textLabel.backgroundColor needed to be set AFTER the cell's background color. I have NO idea why I need to do this, but now it's working. Thanks Mike!