Question

I've been searching a ton and can not seem to find an answer...

I have a tableView which scrolls all the days of all the months. And I have a titleForHeaderInSection: and viewForHeaderInSection: which displays a section header at the top of each month-section which replaces appropriately at the top, cool. But how can I create a single header where the titleLabel changes to the proper month as I scroll (in other words, Not have multiple section headers, just one)?

I can't tell whether this should be setup as "one (big) section with a header that changes" or "a headerView that changes", or whatever... Any examples would be greatly appreciated. Thank you in advance.

Was it helpful?

Solution

You can create a static view and place it above your tableView. Then you can use the visibleCells property to determine which cells are visible. When those cells change to the cells of a new section you can update the static view that is above the UITableView.

Once you are the delegate of the UITableView you are also the delegate of the UIScrollView it is built on.

So you can catch the scrollViewDidScroll: delegate call and update your header there.

So to lay it out:

1.) Create a Header View above your UITableView

2.) Become the delegate of the UITableView

3.) Implement the scrollViewDidScroll: method to detect scrolls

4.) Inside the scrollViewDidScroll method use the visibleCells property of the UITableView to determine which cells are being shown and to update your header view appropriately.

I've never done anything like this, but I don't see why it wouldn't work.

EDIT: As Husker Jeff pointed out. It is probably easier to use indexPathsForVisibleRows to determine what is on screen. You can then use that fairly easily to determine which section/sections are being displayed. Thanks Jeff!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top