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.
You can create a static view and place it above your tableView. Then you can use the
visibleCellsproperty 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 theUITableView.Once you are the
delegateof theUITableViewyou are also thedelegateof theUIScrollViewit 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
UITableView2.) Become the
delegateof theUITableView3.) Implement the
scrollViewDidScroll:method to detect scrolls4.) Inside the
scrollViewDidScrollmethod use thevisibleCellsproperty of theUITableViewto 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
indexPathsForVisibleRowsto determine what is on screen. You can then use that fairly easily to determine which section/sections are being displayed. Thanks Jeff!