4
votes

I would like to create a TRANSLUCENT grouped table view cell. In other words I want to see the grouped table view background pattern, but i don't want completely clear cells. I've seen a lot of questions about transparent cells but none address making translucent (only partially transparent) cells.

This is what I'm trying:

- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.contentView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.5f];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
}

And this is the result: enter image description here

It's almost right, except that the contentView of the cell extends beyond the rounded corners of the grouped cell.

SOLVED by using a transparent image and setting the cell's background view. Would have still liked to do it programmatically though, so if anyone has a solution, I will gladly accept it.

SOLVED Part II Can also be done by setting backgroundView to a new UIView, that has the desired background color and rounded corners via QuartzCore's setCornerRadius call to the view's layer property.

2
the backgroundView property takes a UIView so just there is no need to use the image. Just create a view with the colour you want and set its alpha to the transparency that works for you.Rog
This is basically the same as what I tried initially (there is no difference assigning backgroundView to a new UIView or simply assigning a color to it directly). No rounded corners.Keller
BUT, it occurred to me it can be achieved with a QuartzCore setCornerRadius call :)Keller
Just seen another question related to this... could you have SOLVED part III by simply removing the backgroundView?jrturton

2 Answers

3
votes

This should be all you need:

cell.contentView.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
3
votes

For anyone else wondering, the code that I ended up using was:

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

    //...  

    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    UIView *bgView = [[UIView alloc] init];
    [[bgView layer] setCornerRadius:10.0f];
    [bgView setBackgroundColor:[UIColor colorWithWhite:1.0f alpha:0.25f]];
    cell.backgroundView = bgView;

    //...

    return cell;
}