views:

224

answers:

2

I am trying to create a transparent table with an alpha value. In my main loop, I create a background image, add it to the main window, then create a UITableView with a backgroundColor that is transparent and add it to the main window. For some reason the tableView background color is getting set twice on cells with entries. (i.e. a cell with an entry have an alpha value of 0.8 instead of 0.4)

In my UITableView init method, this is the code that I have

self.tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.4];
[self.tableView setNeedsDisplay];

This works for the most part, except for some weird reason cells that have entries are less transparent than cells that do not have entries

I have tried putting the following in my cellForRowAtIndexPath method: (tried all of the cell's subviews just to be sure)

cell.backgroundView.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
[cell.backgroundView setNeedsDisplay];
[cell.contentView setNeedsDisplay];
[cell.textLabel setNeedsDisplay];

this did not do anything to solve the issue. I've searched extensively online but no one else seems to have run into this issue; any help would be greatly appreciated!

A: 

For some reason, UITableView resets a cell's background color between the tableView:cellForRowAtIndexPath: delegate method and displaying it.

Try implementing tableView:willDisplayCell:forRowAtIndexPath: and setting the cell's background color to transparent there.

A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out.

Ole Begemann
thanks for answering; I still seem to be having the issue though..I tried implementing wilLDisplayCell:forRowAtIndexPath and set the background to clearColor, with no change
GLee11
A: 

If you can relocate your background effect to different view behind your tableView then you can set the backgroundColor for the tableView to clearColor. Which will also be used for the contentView.

(And remember that there are performance reasons for why the default configuration of a table is to use opaque views.)

Michael Dunstan
The problem is that I want the backgroundColor to be opaque, so I set it to have an alpha value of 0.4. What happens is that it applies the opaqueness twice, once to the table background and once for the contentview of the cell, so valid cells have an alpha of 0.8.
GLee11