I did not find an equivalent question on stackoverflow, so I'll post my own question on that problem (please tell me if something is not understandable):
Problem:
I use a common indexed tableView
with section headers from A-Z (like in the contacts.app) in a custom UITableViewController
.
I override tableView:viewForHeaderInSection:
to provide my own section-header views.
When the user scrolls the table up or down, I need to listen to the section headers that appear/disappear at the top or bottom of the tableView (to change some custom view accordingly).
I override these methods (available since iOS 6.0) that do exactly what I need:
tableView:didEndDisplayingHeaderView:forSection:
tableView:willDisplayHeaderView:forSection:
2. is never getting called when scrolling up/down the tableView, whereas method 1 is getting called every time a section header disappears from screen.
I have no idea why the one is getting called and the other is not. Is this a bug from apple or what do I do wrong?
Similar Question:
I've found a similar question here How to call a method “willDisplayFooterView” during Table View cusomization? Where one answer was like:
They will only be called under iOS 6.0 or later and only if you also implement the other header/footer title/view methods. If you are providing a header or footer, there is no need for these to be called
I'm not sure if I understand that answer right, but if so, it seems to be important which methods are implemented in the subclass.
Code:
I post only the non-empty overridden delegate and data-source methods of my tableViewController
:
tvcA.m
@implementation tvcA
...
#pragma mark - Table view data source
- (NSArray *) sectionIndexTitlesForTableView : (UITableView *) tableView
{
// returns sectionIndexArray with alphabet for example
}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section
{
MNSectionHeaderView* headerView = [[MNSectionHeaderView alloc] initWithFrame : CGRectMake(0, 0, 260, 22)];
return headerView;
}
- (NSInteger) tableView : (UITableView *) tableView
sectionForSectionIndexTitle : (NSString *) title
atIndex : (NSInteger) index {...}
...
@end
tvcB.h (inherits from tvcA)
@interface tvcB : tvcA
...
@end
tvcB.m
@implementation tvcB
...
#pragma mark - Table view data source
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView {...}
- (NSInteger) tableView : (UITableView *) tableView
numberOfRowsInSection : (NSInteger) section {...}
- (UITableViewCell*) tableView : (UITableView *) tableView
cellForRowAtIndexPath : (NSIndexPath *) indexPath {...}
- (CGFloat) tableView : (UITableView *) tableView
heightForHeaderInSection : (NSInteger) section {...}
- (UIView *) tableView : (UITableView *) tableView
viewForHeaderInSection : (NSInteger) section {....}
#pragma mark - Table view delegate
- (void) tableView : (UITableView*) tableView
willDisplayHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView*) tableView
didEndDisplayingHeaderView : (UIView*) view
forSection : (NSInteger) section {
// custom configurations
}
- (void) tableView : (UITableView *) tableView
didSelectRowAtIndexPath : (NSIndexPath *) indexPath {...}
...
@end