7

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
4

2 回答 2

10

我想我现在明白了。

上面提到的类似问题已经给出了答案,我没有做对:

[...] 如果您提供页眉或页脚,则无需调用它们

(对于“这些”,他可能意味着我在上面覆盖的两种方法。)

由于我确实提供了自己的节标题,而不是 method tableView:willDisplayHeaderView:forSection:,另一个覆盖方法:tableView:viewForHeaderInSection:每次节标题进入屏幕时都会被调用。

所以最后我会在最后提到的方法中做我需要的配置。

于 2013-06-18T10:48:57.457 回答
0

出于某种原因,我tableView:viewForHeaderInSection:的所有表格的所有部分都被调用(我有一个带有很多表格的滚动视图)。所以你的解决方案对我不起作用。但是,由于我只需要知道弹出 VC 时哪个是第一个可见部分,因此我想出了这个不错的解决方案:

    NSInteger firstVisibleSection = 0;
    for (int i = 0; i < [_board.swimlanes count]; i++)
    {
        CGRect sectionRect = [currentTable rectForSection:i];
        BOOL containsPoint = CGRectContainsPoint(sectionRect, currentTable.contentOffset);
        if (containsPoint)
        {
            firstVisibleSection = i;
            break;
        }
    }

可能对有同样问题的人有帮助:)

于 2014-03-28T17:51:07.657 回答