5

I have created a table view header using the answer here.

My problem is that when creating the header this way, it does not "stick" to the top of my UITableView when scrolling down. If I create my header in code inside of viewForHeader then it does stick.

How can I get my header to stick to the top while scrolling if I create it in storyboard? 2 other askers here and here asked this question and never got an answer.

And yes my UITableView is already plain style, not grouped.

Community
  • 1
  • 1
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • Connect the headerView to your view controller and implement scrollViewDidScroll: method. Then set the frame origin y of the header view offsetting by the scrollview offset. – Sandeep May 17 '14 at 17:03
  • 1
    The reason for the difference in behavior is that creating a header by dragging in a view in IB creates a tableHeaderView, whereas using viewForHeader creates a section header. They are two different things. – rdelmar May 17 '14 at 20:44
  • 1
    While Adam's answer below works, the Apple approved way is to create a NIB file with the desired view. What a PITA. – David H Sep 08 '14 at 15:14

2 Answers2

6

rdelmar's comment helped me understand the reasoning behind it not working. Here is what I did in practice to fix the problem:

I had to drag the header view in storyboard down by where the first responder is. You'll notice you can't actually drag the header view down on the view itself, you'll have to drag the element down from the document outline.

I also had to implement viewForHeaderInSection (returning the header view that I made an outlet) and heightForHeaderInSection.

Every time you want to edit the view via storyboard, you'll have to drag it back up to edit it, then drag it back down so it works correctly.

Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • 1
    Thanks for this. I faced the same situation and was able to resolve it using your answer. Although how you've explained about editing the header isn't very clear. Better mention that dragging in and out should be done using the **Document Outline view**. – Isuru Aug 26 '14 at 08:58
  • The key element you uncovered is how to keep it in the Storyboard, yet be able to edit it when necessary. I've had a bug issue with Apple on this for years - we need the ability to have arbitrary views in Storyboards. – David H Sep 08 '14 at 15:13
  • If you already have section headers - which number section do you respond to? – Alex Brown Dec 12 '14 at 19:42
  • @Adam Johns .... its working fine but i have a little problem...i put searchview in headerview of tableview..and it is stick on top all the time but when i scroll tableview to upper side than the headerview hight goes little small from its top side for bit time and then its come to its actual height? can you help me how can i fixed this problem?? – Bandish Dave Apr 23 '15 at 05:42
  • I tried this as well but to no avail. Mine still scrolls up. – tjb May 17 '16 at 04:57
1

Just drag tableview cell first, then your view for the header above. And afterward, remove the unneeded cell. The header will remember its position.

Vitya Shurapov
  • 2,200
  • 2
  • 27
  • 32