Sean Smyth
Sean Smyth

Reputation: 1581

How can I place a UITableView on a UIViewController programmatically so that it doesn't take up the whole view?

I want to have a ViewController that has a table view on the top half of the screen, and a button under it. I'm assuming there's a simple way to do this. Every way I have tried so far ends up in the bottom half of the view being inaccessible.

Upvotes: 0

Views: 501

Answers (2)

Mundi
Mundi

Reputation: 80265

The trick is that you cannot use a subclass of UITableViewController. You need to enable the datasource and delegate protocols explicitly in a UIViewController.

@interface MyController : UIViewController <UITableViewDatasource, UITableViewDelegate>
// variables and properties
@end

Don't forget to assign self as delegate and datasource of your tableview.

Now you can just put the table view anywhere by giving it an appropriate frame and you can also put a button at the bottom.

Hint: the standard way to "put a button at the bottom" is to use a UIToolBar. With that you could use the normal table view controller without having to worry about the protocols. But your approach is also feasible.

Upvotes: 1

Praveen-K
Praveen-K

Reputation: 3401

Well, you could have put a sample image to get the clear idea. but AFAI understood, i try to give the answer accordingly :

You can set the frame especially height of TableView just half the size of the height of your UIViewController.

Then rest of the space of UIViewCobtroller you can create another view where you can place your button as you have already done so far.

You can add the button in your UITableView's footer section.

Upvotes: 0

Related Questions