Matthew Bischoff
Matthew Bischoff

Reputation: 1043

Make a cell's background transparent, without also making the text transparent

I have a UITableView that I want to have a transparent background, but I can't seem to get that without making the text in the cells transparent as well. Here is what I have.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSString *labelString = @"Hello";

[cell setBackgroundColor:[UIColor darkGrayColor]];
cell.alpha 0.2f;

cell.textLabel.textcolor = [UIColor whiteColor];
cell.textLabel.text = labelString;

I have tried several other things such as setting the contentView's background color and alpha, as well as the backgroundView's color and alpha, nothing seems to work.


Upvotes: 1

Views: 2100

Answers (2)

Matthew Bischoff
Matthew Bischoff

Reputation: 1043

Placing this code in the -tableView:willDisplayCell:forRowAtIndexPath: solved the issue.

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

Upvotes: 2

Peter DeWeese
Peter DeWeese

Reputation: 18333

Instead of setting the cell.alpha,

cell.backgroundColor = [UIColor clearColor];

You may also need to set the table view background.

Upvotes: 4

Related Questions