UITableView background color getting set twice
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.back开发者_如何学GogroundView.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!
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.
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.)
I had the same problem the only thing that worked for me was this:
tableView.backgroundView = [[[UIView alloc]init]autorelease];
精彩评论