开发者

iphone SDK: How to add image subview to UITableViewController and still see cells?

I'm adding an image subview to an UITableViewController with a grouped table. Once I add the image subview I can see the table headers on top of the image, but the cells are being drawn behind the image.

Is there something else I need to do to ensure that the table cells appear on top of the background image? Here is the code I'm using in the UITableViewControlle's viewDidLoad:

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 959.0f); 
UIImageView *backImageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
[backImageView setImage:[UIImage imageNamed:@"somebackground.png"]]; 
[self.view addSubview:backImageView];
[self.view sendSubviewToBack:backImageView];
[backImageView release];

I realize that as an alternative I could simply create a composite view and drop the tableview on top开发者_JAVA百科 of it rather than what I am trying to do here. If there isn't any other solution I will do so, but would prefer to use the approach I'm trying if at all possible.


It's a grouped table view. From the apple docs:

The grouped style of table view provides a default background color and a default background view for all cells.

This means that there is an default image view being inserted behind your table. You want to replace that with your own view, like so:

CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 959.0f); 
UIImageView *backImageView = [[UIImageView alloc] initWithFrame:myImageRect]; 
[backImageView setImage:[UIImage imageNamed:@"somebackground.png"]]; 
self.tableView.backgroundView = backImageView;

This replaces the other backgroundView with your own. Otherwise when the table view is laid out again, that other background image will overlay yours.


It appears that the UITableView will often rearrange the zorder of views when refreshing. So, you need to manually manage resetting the zorder on any table events such as inserting/deleting/updating rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜