Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS How to add STATIC UITableView and another view to the same view controller

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!

like image 798
lnafziger Avatar asked Jan 06 '12 19:01

lnafziger


2 Answers

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.

like image 146
Mark Krenek Avatar answered Sep 21 '22 15:09

Mark Krenek


I am not sure if it works, but you may want to try this approach too:

  1. Create a UIViewController, e.g. BaseViewController (both a class and in Storyboard)
  2. Create a UITableViewController e.g. MyStaticTableViewController (only in Storyboard and add an identifier to it)
  3. In Storyboard, add an instance of UIView with appropriate dimensions to your BaseViewcontroller, e.g. myStaticTableView.
  4. Programmatically, add the view of MyStaticTableViewController to myStaticTableView.
like image 45
Canopus Avatar answered Sep 19 '22 15:09

Canopus