【问题标题】:tableView:willDisplayHeaderView:inSection: not called when running for iOS 6.1tableView:willDisplayHeaderView:inSection: 为 iOS 6.1 运行时未调用
【发布时间】:2013-06-10 14:49:50
【问题描述】:

我没有在 stackoverflow 上找到等效的问题,所以我将就该问题发布我自己的问题(如果有什么不明白的地方请告诉我):

问题:

我在自定义UITableViewController 中使用带有A-Z 部分标题的通用索引tableView(如在contacts.app 中)。 我覆盖 tableView:viewForHeaderInSection: 以提供我自己的节标题视图。

当用户向上或向下滚动表格时,我需要听取在 tableView 顶部或底部出现/消失的部分标题(以相应地更改一些自定义视图)。

我重写了这些方法(自 iOS 6.0 起可用),它们完全符合我的需要:

  1. tableView:didEndDisplayingHeaderView:forSection:

  2. tableView:willDisplayHeaderView:forSection:

2。向上/向下滚动 tableView 时永远不会调用方法 1,而每次节标题从屏幕上消失时都会调用方法 1。

我不知道为什么一个被调用而另一个没有。这是苹果的错误还是我做错了什么?


类似问题:

我在这里发现了一个类似的问题 How to call a method “willDisplayFooterView” during Table View cusomization? 一个答案是这样的:

它们只会在 iOS 6.0 或更高版本下被调用,并且只有当您还实现了其他页眉/页脚标题/视图方法时。如果您提供页眉或页脚,则无需调用它们

我不确定我是否理解这个答案,但如果是这样,在子类中实现哪些方法似乎很重要。


代码:

我只发布了我的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(从 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

【问题讨论】:

    标签: iphone objective-c ios6 uitableview indexed


    【解决方案1】:

    我想我现在明白了。

    上面提到的Similar Question刚刚已经给出了答案,我没有答对:

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

    (“这些”可能是指我上面覆盖的两种方法。)

    因为我确实提供了自己的节标题,而不是方法 tableView:willDisplayHeaderView:forSection:,另一种覆盖方法:tableView:viewForHeaderInSection: 在每次节标题进入屏幕时被调用。

    所以最后我将按照最后提到的方法进行我需要的配置。

    【讨论】:

      【解决方案2】:

      出于某种原因,在我的情况下,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;
              }
          }
      

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

      【讨论】:

      • 我假设,您只想跟踪屏幕上可见的最顶部部分,对吧?当然:tableView:viewForHeaderInSection: 每次在屏幕上出现(重新)部分标题时都会被调用 - 在顶部和/或底部。跟踪顶部的一个更简单的方法是:添加一个类属性,如NSUInteger topSection,并将其设置在tableView:viewForHeaderInSection: 中,如:if(section &lt; _topSection){ _topSection = section;}tableView:didEndDisplayingHeaderView:forSection:nextSectionShown:if(section == _topSection) {_topSection = nextSection;}。试试看 - 它更容易
      • 我试过了 - 它不起作用。至少在我的情况下。我有一个分页滚动视图,每个页面都有一个表格。当视图第一次出现时,tableView:viewForHeaderInSection: 会调用所有 headerView,而不仅仅是屏幕上的那些。此外,使用您建议的解决方案,您可以一直跟踪该部分,而使用我在上面发布的解决方案,您只需检查何时需要它。
      • 好的,我认为这两种解决方案(检查何时需要它/始终跟踪)在不同的情况下可能会有所帮助(在我的情况下,我需要一直更新这些信息)。只是因为我很好奇:第一次加载分页滚动视图/表格时,它不总是显示在顶部的第一部分0 吗?而且 - 顺便说一句:你知道你可以检查哪个 tableView 方法 tableView:viewForHeaderInSection: 被调用,如:if (tableView == self.searchDisplayController.searchResultsTableView)...
      • 不,当 VC 加载时,我会关注上一个会话中最后查看的泳道。这就是我需要的信息;)这就是为什么我只在最后才需要它。我并不是说我的解决方案是唯一的,但事实证明它对我来说是完美的,所以我在这里提供它。
      猜你喜欢
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多