Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tableView:willDisplayHeaderView:inSection: not called when running for iOS 6.1

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:

  1. tableView:didEndDisplayingHeaderView:forSection:

  2. 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
like image 569
anneblue Avatar asked Jun 10 '13 14:06

anneblue


1 Answers

I think I got it now.

The Similar Question mentioned above just already gives the answer, which I didn't get right:

[...] If you are providing a header or footer, there is no need for these to be called

(With "these" he might have meant the two methods I override above.)

As I do provide own section headers, instead of the method tableView:willDisplayHeaderView:forSection:, the other overriding method: tableView:viewForHeaderInSection: is getting called each time a section header enters the screen.

So finally I will just do my needed configurations in the last mentioned method.

like image 186
anneblue Avatar answered Sep 21 '22 11:09

anneblue