简体   繁体   中英

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.


Instead of setting the cell.alpha,

cell.backgroundColor = [UIColor clearColor];

You may also need to set the table view background.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM