0

I've created a view (with a searchbar and some buttons in it) to my UITableViewController. I've then created an outlet for the view in my ViewController.h file and synthasized it in my ViewController.m file.

Here's a screen shot of my current setup

enter image description here

I'm using

self.tableView.tableHeaderView = headerView;

And I'm using

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return headerView;
}

But my view still scrolls up and down with the table, it doesn't seem to be set as the header properly?

Phil
  • 2,995
  • 6
  • 40
  • 67

1 Answers1

0

"it doesn't seem to be set as the header properly" -- yes it is being set properly. That is how a table header behaves. If you don't want it to scroll, then you can do one of two things. Either add the view as a sibling view to a UIViewController (your view above, with the table view below), or, if you only have one section, you can use it as a section header which will stick to the top.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • How can I set it to the section header? – Phil Jan 14 '15 at 16:37
  • @Phil, you implement tableView:viewForHeaderInSection – rdelmar Jan 14 '15 at 16:39
  • @Phil, remove the line, self.tableView.tableHeaderView = headerView; I don't know if that is interfering with your implementation of viewForHeaderInSection. You also should implement heightForHeaderInSection. – rdelmar Jan 14 '15 at 16:46
  • I removed that lined as suggested, but it's still scrolling up and down. – Phil Jan 14 '15 at 16:56
  • @Phil, that's because you added the view to the table view in the storyboard, which automatically makes it a table header view (even if you take out that line). You should make that view in a xib file (you can copy and paste it into one), and register that xib ( registerNib:forHeaderFooterViewReuseIdentifier:). In viewForHeader InSection, you would then use dequeueReusableHeaderFooterViewWithIdentifier: to get your view. Be sure to implement tableView:heightForHeaderInSection: or the height will be 0. – rdelmar Jan 14 '15 at 17:01
  • I'll try that thanks, but aren't we all meant to just use storyboards now and not xib files? – Phil Jan 14 '15 at 17:03
  • @Phil, no, there's no reason not to still use xibs (along with storyboards). – rdelmar Jan 14 '15 at 17:04
  • Would it better to create the view and contents programmatically? – Phil Jan 14 '15 at 17:05
  • @Phil, that's up to you; do it however you want. I have seen people do it in the storyboard, using a cell prototype to make their header view -- see the accepted answer here (I don't know if this is the right way to go) http://stackoverflow.com/questions/9219234/how-to-implement-custom-table-view-section-headers-and-footers-with-storyboard – rdelmar Jan 14 '15 at 17:07
  • I've tried to follow the .nib approach, but I've run into an error as described here http://stackoverflow.com/questions/27965363/invalid-nib-registered-for-identifier – Phil Jan 15 '15 at 14:15