【问题标题】:UITableView : viewForHeaderInSection: not called during reloadData:UITableView:viewForHeaderInSection:在 reloadData 期间未调用:
【发布时间】:2013-02-11 06:58:25
【问题描述】:

我已经使用正确的委托和数据源链接设置了表格视图。reloadData 方法调用数据源和委托方法,viewForHeaderInSection: 除外。

为什么会这样?

【问题讨论】:

  • heightForHeaderInSection: 实现了吗?
  • 您是否为 TableView 的sectionHeaderHeight 设置了值?

标签: ios uitableview uitableviewsectionheader


【解决方案1】:

tableView:viewForHeaderInSection: 的使用要求您还实现tableView:heightForHeaderInSection:。这应该为标题返回一个适当的非零高度。还要确保你没有同时实现tableView:titleForHeaderInSection:。您应该只使用其中一个(viewForHeadertitleForHeader)。

【讨论】:

  • 确保方法的签名没有错别字。一个错误的字母将意味着它不会被调用。也检查一下情况。还要确保您从 numberOfSections 返回 0。
  • 一切都是正确的并且编译正确..我想了解的问题是调用该方法的时间.. tableView:viewForHeaderInSection 在表格即将显示时被调用而不是作为 [tableview reloadData] 同步执行的一部分
  • @maddy OMG 谢谢,我太愚蠢了,我创建了我的实例,但我没有附加到我的数组中
  • 你绝对可以两者兼得。 viewForHeaderInSection: 将优先于 titleForHeaderInSection: 唯一的要求是您在 table view 上设置estimateSectionHeaderHeight 与 0 不同的值,否则 viewForHeaderInSection: 将永远不会被调用
  • 添加到@romrom 的评论:如果您同时实现了titleForHeaderInSection:viewForHeaderInSection:,并且从后者返回的视图是UITableViewHeaderFooterView 的子类,那么它的textLabel.text 会自动设置为titleForHeaderInSection: 字符串的全大写版本。为了防止这种行为,要么不实现titleForHeaderInSection:,要么使用自定义标签而不是继承的textLabel
【解决方案2】:

诀窍在于这两个方法属于不同的UITableView 协议:tableView:titleForHeaderInSection: 是一个UITableViewDataSource 协议方法,其中tableView:viewForHeaderInSection 属于UITableViewDelegate强>。

这意味着:

  • 如果您实现了这些方法但仅将自己指定为 dataSourceUITableView,您的 tableView:viewForHeaderInSection 实现将被忽略。

  • tableView:viewForHeaderInSection 具有更高的优先级。如果你 实现这两种方法并将自己指定为 dataSourcedelegate 对应 UITableView,您将 返回部分标题的视图,但您的 tableView:titleForHeaderInSection: 将被忽略。

我也试过删除tableView:heightForHeaderInSection:;它工作正常,似乎没有影响上述程序。但是文档说tableView:viewForHeaderInSection 需要它才能正常工作;所以为了安全起见,实施这一点也是明智之举。

【讨论】:

  • 你成就了我的一天!!!忘记将UITableViewDelegate 分配给self,因为我认为tableView:viewForHeaderInSectionUITableViewDataSource 方法。谢谢!
  • "tableView:viewForHeaderInSection" 并不重要。重要的是您以某种方式返回一个高度。您可以通过 1. 估计或 2. 硬编码值或 3. 具有内在大小的 titleForHeader 来实现这一点。固有大小是根据字体系列和大小计算得出的。
【解决方案3】:

@rmaddy 两次错误地陈述了规则:实际上,tableView:viewForHeaderInSection: 确实 要求您也实现 tableView:heightForHeaderInSection:,而且调用 titleForHeader 和 @ 也完全没问题987654324@。为了记录,我将正确地陈述规则:

规则很简单,除非您以某种方式给标题指定高度,否则不会调用viewForHeader。您可以通过以下三种方式的任意组合来执行此操作:

  • 实现tableView:heightForHeaderInSection:

  • 设置表的sectionHeaderHeight

  • 调用titleForHeader(如果标题没有默认高度,这会以某种方式为标题提供默认高度)。

如果你不做这些事情,你将没有标题,viewForHeader 将不会被调用。那是因为没有高度,运行时将不知道如何调整视图的大小,所以它不会费心去询问。

【讨论】:

  • 来自tableView:viewForHeaderInSection: 的文档:“此方法仅在同时实现tableView:heightForHeaderInSection: 时才能正常工作。”。
  • 很好。医生说什么,他们说什么。现在做实验。事实如我所说。
  • 你怎么能同时拥有titleForHeaderInSectionviewForHeaderInSection?表格视图只会调用两者之一(我现在忘记了哪个优先)。
  • 实际上还有一个难题,那就是 有时 viewForHeader 被称为 没有 这三种分配高度。我遇到过这种情况,我的viewForHeader 被调用并且标题显示得很好,直到有一天,我没有改变,他们没有。那时我开始尝试发现调用viewForHeader 的最低要求是什么。现在我知道了。现在你也是。
  • @texas 不,我不做 xamarin。在 Cocoa 框架之上添加另一个级别的间接性只会让我大吃一惊。 :)
【解决方案4】:

您应该实现tableView:heightForHeaderInSection: 并将标题的高度设置为>0。

此委托方法与viewForHeaderInSection: 方法一起使用。

我希望这会有所帮助。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
         return 40;
}

【讨论】:

  • 您是否测试过您的答案确实有效?因为如果您仔细阅读,则说明最初调用了 viewForHeaderInSection。只有在重新加载表时才调用它!
【解决方案5】:

我刚刚遇到了一个问题,即 iOS 7.1 的标头没有显示,但在我测试过的更高版本中运行良好,特别是在 8.1 和 8.4 中。

对于完全相同的代码,7.1根本没有调用任何节头委托方法,包括:tableView:heightForHeaderInSection:tableView:viewForHeaderInSection:

经过实验,我发现从我的viewDidLoad删除这一行会使标头重新出现在 7.1 中,并且不会影响我测试的其他版本:

// _Removing_ this line _fixed_ headers on 7.1
self.tableView.estimatedSectionHeaderHeight = 80;

……所以,至少在 7.1 中似乎存在某种冲突。

【讨论】:

    【解决方案6】:

    值得注意的是,如果您的tableView:heightForHeaderInSection: 实现返回UITableViewAutomaticDimension,那么tableView:viewForHeaderInSection: 将不会被调用。

    UITableViewAutomaticDimension 假定将使用一个标准的 UITableViewHeaderFooterView,并填充了委托方法 tableView:titleForHeaderInSection:

    来自UITableView.h中的cmets:

    如果标题不为零,则从 tableView:heightForHeaderInSection:tableView:heightForFooterInSection: 返回此值会产生适合从 tableView:titleForHeaderInSection:tableView:titleForFooterInSection: 返回的值的高度。

    【讨论】:

    • 如果您将estimatedSectionHeaderHeight设置为某个值,tableView:viewForHeaderInSection 将被调用(类似于行的自动尺寸工作原理)
    • 很有趣,谢谢。回到 7.1 this subtlety of estimated height was important for cells,标题也可能是这种情况——但现在已经不是很相关了!
    【解决方案7】:

    有时在viewWillAppear:viewDidAppear: 方法中设置tableview.delegatedatasource = nil 可能会导致此问题。确保不要这样做...

    【讨论】:

      【解决方案8】:

      离开 rmaddy 的回答,我试图隐藏 Header 视图并返回 0.0f 的 "tableView:heightForHeaderInSection" 和来自 tableView:viewForHeaderInSection 的 0 高度视图。

      tableView:heightForHeaderInSection中的return 1.0f改成return 0.0f后,确实调用了委托方法tableView:viewForHeaderInSection

      结果我想要的效果不需要使用“tableView:heightForHeaderInSection”;但这对于在调用“tableView:heightForHeaderInSection”委托方法时遇到问题的其他人可能很有用。

      【讨论】:

        【解决方案9】:

        提供 estimatedSectionHeaderHeightsectionHeaderHeight 值解决了我的问题。 例如。, self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension

        【讨论】:

        • 我的问题是在升级到 Swift 3.1 后开始的。这个解决方案修复了它。
        • @pbuchheit Apple Docs 说它适用于 iOS 7.0+,请看这里,developer.apple.com/documentation/uikit/uitableview/…
        • @Sharukh Mastan 看起来你是对的。出于某种原因,我在尝试使用该属性时收到了警告,但在进行干净构建后它就消失了。
        【解决方案10】:

        我将以下两个方法从 Swift 2 项目中剪切并粘贴到我的 Swift 3 项目中,它们从未被调用,因为在 Swift 3 中,这些方法的第一个参数名称前必须有“-”。

        func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 44.0
        }
        
        func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
            let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: B2BTrolleyHeaderFooterView.reuseIdentifier) as! B2BTrolleyHeaderFooterView        
            return headerView
        }
        

        【讨论】:

          【解决方案11】:

          同样的问题发生在我身上,但由于我使用 xCode 9 中的 自动高度计算,我无法给出上述任何明确的高度值。经过一些实验,我得到了解决方案,我们必须重写这个方法,

          -(CGFloat)tableView:(UITableView *)tableView 
                   estimatedHeightForHeaderInSection:(NSInteger)section
          {
                return 44.0f;
          }
          

          虽然我已经检查了两个选项

          1. 自动高度计算
          2. 自动估计高度计算

          正如苹果所说,来自 storyboard,但我仍然收到这个奇怪的错误。

          请注意:此错误仅在 IOS-10 版本上显示,而不在 IOS-11 版本上显示。也许这是来自 xCode 的错误。谢谢

          【讨论】:

            【解决方案12】:

            这是我发现的 (Swift 4) (感谢this comment另一个问题)

            无论我使用 titleForHeaderInSection 还是 viewForHeaderInSection - 并不是在滚动表格视图和加载新单元格时它们没有被调用,而是我为 headerView 的 textLabel 所做的任何字体选择都只出现在最初的内容上在加载时可见,而不是在表格滚动时可见。

            修复是 willDisplayHeaderView:

            func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
                if let header = view as? UITableViewHeaderFooterView {
                    header.textLabel?.font = UIFont(name: yourFont, size: 42)
                }
            }
            

            【讨论】:

              【解决方案13】:

              就我而言,我使用UITableviewCell 创建了标题视图,并像这样返回viewForHeaderInSection 中的单元格

              return cell
              

              改成

              return cell.contentView 
              

              为我工作。

              【讨论】:

                【解决方案14】:

                就我而言

                viewForHeaderInSection
                

                是在一个很远的派生类中实现的,它不会打扰到超类中。

                【讨论】:

                  【解决方案15】:

                  viewForHeaderInSection 没有被调用的原因有两个:

                  要么你没有设置你的UITableViewDelegate,要么 你错误地设置了你的UITableViewDelegate

                  【讨论】:

                    【解决方案16】:

                    就我而言,这是因为我没有实施:

                    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-06-21
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2021-07-04
                      • 1970-01-01
                      相关资源
                      最近更新 更多