Skip to content

Instantly share code, notes, and snippets.

@brockboland
Last active December 18, 2015 20:58
Show Gist options
  • Save brockboland/5843523 to your computer and use it in GitHub Desktop.
Save brockboland/5843523 to your computer and use it in GitHub Desktop.
I finally figured out how to include another view at the top of a UITableViewController. I think this will be necessary (or at least helpful) to mimic the Contact edit view in the Contacts app.
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Replace "HeaderSubViewNibName" with the actual name of the xib file to load
// your header view.
// This returns an array, hence the objectAtIndex: to get the first (and only)
// item in the array.
UIView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderSubViewNibName" owner:self options:nil] objectAtIndex:0];
// This is an alternate option, to load a ViewController within the storyboard.
// The Storyboard ID must be set on the view controller in the Identity Inspector.
// Note that this will fail since headerView is already declared above: choose one
// or the other.
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"HeaderSubViewController"];
UIView *headerView = vc.view;
// Set the width and height here as desired. Otherwise, it will appear
// full-height, pushing the TableView below down off the visible portion of
// the view.
[headerView setFrame:CGRectMake(0, 0, 320, 100)];
// Replace this to configure the header view as needed. In my testing, I added
// a UIImageView to the header sub-view, so I set the image to show.
UIImageView *img = (UIImageView*)[headerView viewWithTag:1111];
[img setImage:[UIImage imageNamed:@"placeholder.jpg"]];
// Set the header view.
[self.tableView setTableHeaderView: headerView];
}
@brockboland
Copy link
Author

My problem right now is that the UITableViewController is in use in a storyboard, inside of a navigation controller. I'd like to be able to segue to a new view controller when an object in the header view is tapped, but since that view is outside the navigation controller workflow, it doesn't work. Not sure how to solve this.

@brockboland
Copy link
Author

Quick update: it turns out that you can drag a View directly into a UITableViewController

And with a label in that view:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment