0

I have a UITableView with many sections. It is a simple table view. I am using viewForHeaderInSection to create custom views for these headers. So far, so good.

The default scrolling behavior is that when a section is encountered, the section header stays anchored below the Nav bar, until the next section scrolls into view.

My question is this: can I change the default behavior so that the first section header stay anchored at the top for all sections?

Thanks.

Desired Behavior image url:

https://www.dropbox.com/s/2lnspddx02aku1n/scroll.png?dl=0

Community
  • 1
  • 1
riven
  • 73
  • 9
  • I doubt you'll be able to do this in any straightforward way. You'll probably need to subclass UITableView and add the special first-section-header view as an "anchored" view. – RobP Feb 20 '15 at 22:37
  • this might help you. if it does, you can delete this question. http://stackoverflow.com/questions/5441938/adding-ios-uitableview-headerview-not-section-header – danh Feb 20 '15 at 22:40
  • @danh I'm hoping that the header view does not scroll up with the rest of view as tableHeaderView does.. – riven Feb 20 '15 at 22:56
  • Do you want that first section header to scroll at all? – AdamPro13 Feb 20 '15 at 22:56
  • @riven, I think what AdamPro13 is getting at is, that if you don't want the section header behavior, and you don't want the tableView header behavior, then maybe what you really want is an independent view that sits over the table. It needn't be related to the table at all. – danh Feb 20 '15 at 23:02
  • This question looks like it needs a picture or two to explain the desired behavior, because it's very unclear. – nhgrif Feb 20 '15 at 23:14
  • @danh that's exactly what I was getting at. Add the "first section header" view as a subview of the table view and adjust the contentInset of the table view so that the content will start under the "section header" when the user is scrolled to the top of the table view. – AdamPro13 Feb 20 '15 at 23:28
  • Could you please take a look at this image: https://www.dropbox.com/s/2lnspddx02aku1n/scroll.png?dl=0 – riven Feb 20 '15 at 23:28

1 Answers1

0

I've done this by augmenting the header/data relationship. You can have a section header on even numbered sections, and data in odd numbered sections.

Example: For section 0, you can show the appropriate heading, but have 0 rows in that section. The data for section 0 should show up in section 1. Section 1 will not have a header. etc etc.

Roderic Campbell
  • 719
  • 5
  • 14
  • I'm not sure how this could make the first section header stick to the top. – riven Feb 21 '15 at 00:05
  • I saw the dropbox image now. That clarified it a bit for me. Is there any separator between the first and second sections? Dropbox image has a black line. What if you just had 1 section and the cells in all other sections are basically in the first section. Would that solve it? – Roderic Campbell Feb 22 '15 at 03:50
  • That occurred to me as well. However I also want the index bar feature of tableview, which can only be done if there are multiple sections... @Roderic Campbell – riven Feb 22 '15 at 22:15
  • In that case perhaps you could use a custom index bar implementation, perhaps similar to this: https://github.com/gdavis/GDIIndexBar – Roderic Campbell Feb 23 '15 at 20:23