Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a subview in a UITableViewController

I am using a UITableViewController, I have resized the height of the table view and moved it a little down. Now I'd like to add a UIImageView above that table view but the only thing that I can do is add that UIImageView as a subview of the UITtableView of the UITableViewController, so that UIImageView is not above the UITableView and the image scrolls with the table view.

How can I accomplish what I'm trying ? Here is the code I've been using :

UIImageView *tabImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"temp.png"]];
[self.view addSubview:tabImageView];

Thanks!

like image 920
MartinMoizard Avatar asked Dec 09 '10 21:12

MartinMoizard


2 Answers

I believe UITableViewController requires that its view be a UITableView.

In the past, I've worked around this by changing my view controller from subclassing UITableViewController to subclassing UIViewController and implementing the UITableViewDataSource and UITableViewDelegate protocols. Then you can have your view be whatever you want, including shifting your UITableView down and adding a UIImageView.

Not sure if this is still the case in 4.2, though.

like image 163
Shaggy Frog Avatar answered Oct 14 '22 01:10

Shaggy Frog


EDIT (December 16, 2013)

Just want to clarify that this is really not a great idea for many reasons, a bunch of which are listed in the comments. You should really be doing what Shaggy Frog is suggesting, or using View Controller Containment.

Original Answer

You want the UIImageView to be static at the top of the tableView? If that's so, you need to set it as a subview of the UINavigationController, not the UITableView. Easiest way to do this is with [self.view.superview addSubview:tabImageView]. (although as Shaggy Frog said, it's probably best to change from a UITableViewController to a plain UIViewController, and create the UITableView manually)

If you want the UIImageView to be attached to the UITableView, so that when you scroll down, it disappears, you can just set it as the table header with [self.tableView setTableHeaderView:tabImageView]

like image 9
Gordon Fontenot Avatar answered Oct 14 '22 02:10

Gordon Fontenot