Reputation: 25740
When I create a static table view and put it in a normal view controller, I get an error which says that static table views must be in UITableViewController's.
What I want is a view on the iPad which has a static table view which takes up most of the screen, but a standard UIView under it which always stays visible and has a couple of labels which I update based on content in the table view.
Is there a way to use a UITableViewController and have the tableView not be full screen so that I can add a subview to it in a Storyboard?
If no, can it be done in code?
Any tips would be appreciated!
Upvotes: 5
Views: 7160
Reputation: 4947
What you need is a regular UITableViewController
(so you get all the static cell behavior) and then your view as a subview of the table view, but managed such that it always sticks to the bottom of the screen. Apple demonstrated this as WWDC. Here's what I do (in my example, I have a UIDatePicker
that I always want anchored at the bottom):
In viewDidLoad
, create your view and add it as a subview of the table view. Place it at the bottom of the table view.
[self.tableView addSubview:self.datePicker];
[self updateDatePickerBounds];
Implement scrollViewDidScroll
so that you can force your view to the bottom of the screen ever time the table view scrolls:
- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
// Keep the date picker floating above the table view, anchored to the bottom of the view
[self updateDatePickerBounds];
}
And then implement a function that positions your view correctly:
- (void) updateDatePickerBounds
{
// Keep the date picker floating above the table view, anchored at the bottom
CGRect tableBounds = self.tableView.bounds;
CGRect pickerFrame = self.datePicker.frame;
pickerFrame = CGRectMake(tableBounds.origin.x,
tableBounds.origin.y + CGRectGetHeight(tableBounds) - CGRectGetHeight(pickerFrame),
tableBounds.size.width,
pickerFrame.size.height);
self.datePicker.frame = pickerFrame;
}
Finally, if you don't want your table view going under your bottom view, just set the table view's contentInset
to match the height of your view.
I also call my updateDatePickerBounds
method from viewWillAppear
. I forget why I had to do that.
Upvotes: 6
Reputation: 1367
The real answer is to create a custom container controller.
For instance, you can have a static table view inside a nav controller. So, you need to create a container class, like a nav controller or whatever yours is, and add the static table view controller to that.
Upvotes: 0
Reputation: 3727
I am not sure if it works, but you may want to try this approach too:
Upvotes: 2
Reputation: 14427
Static cell TableViews must be of type UITableViewController, but you can achieve the same effect by using a ViewController with a TableView (or 2) and take care of the cellForRowAtIndexPath method yourself for the TV you want to be "Static Cell". Just switch the IndexPath.section and .row and build the cell the way you want it. More coding sure, but you get the same effect and with more flexibility if you don't want to do it Apple's Static Cell way.
Upvotes: 1
Reputation: 30846
You can achieve the same effect using UITableViewController
and a static UITableView
. Create the view you want at the bottom of the table and set the tableFooterView
property on UITableView
to be that view. This can be done by dragging a UIView
object out of the library and adding it to the bottom of the table view (you'll notice the bottom area highlight in blue to know you're doing it correctly). You may have to adjust the content insets to get it to display how you want.
Upvotes: 1