ios UITableView : viewForHeaderInSection: 在 reloadData 期间未调用:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15078725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:27:11  来源:igfitidea点击:

UITableView : viewForHeaderInSection: not called during reloadData:

iosuitableviewuitableviewsectionheader

提问by inforeqd

I've set up the tableview with correct delegate and datasource linkages.. the reloadData method calls the datasource and the delegate methods except for viewForHeaderInSection:.

我已经使用正确的委托和数据源链接设置了 tableview.. reloadData 方法调用数据源和委托方法,除了viewForHeaderInSection:.

Why is that so?

为什么呢?

回答by rmaddy

The use of tableView:viewForHeaderInSection:requires that you also implement tableView:heightForHeaderInSection:. This should return an appropriate non-zero height for the header. Also make sure you do not also implement the tableView:titleForHeaderInSection:. You should only use one or the other (viewForHeaderor titleForHeader).

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

回答by Yunus Nedim Mehel

The trick is that those two methods belong to different UITableViewprotocols: tableView:titleForHeaderInSection:is a UITableViewDataSourceprotocol method, where tableView:viewForHeaderInSectionbelongs to UITableViewDelegate.

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

That means:

这意味着:

  • If you implement the methods but assign yourself only as the dataSourcefor the UITableView, your tableView:viewForHeaderInSectionimplementation will be ignored.

  • tableView:viewForHeaderInSectionhas a higher priority. If you implement both of the methods and assign yourself as both the dataSourceand the delegatefor the UITableView, you will return the views for section headers but your tableView:titleForHeaderInSection:will be ignored.

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

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

I have also tried removing tableView:heightForHeaderInSection:; it worked fine and didn't seem to affect the procedures above. But the documentation says that it is required for the tableView:viewForHeaderInSectionto work correctly; so to be safe it is wise to implement this, as well.

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

回答by matt

@rmaddy has misstated the rule, twice: in reality, tableView:viewForHeaderInSection:does notrequire that you also implement tableView:heightForHeaderInSection:, and also it is perfectly fine to call both titleForHeaderand viewForHeader. I will state the rule correctly just for the record:

@rmaddy说错了规则,两次:在现实中,tableView:viewForHeaderInSection:不是要求您还实现了tableView:heightForHeaderInSection:,而且它是完全没有同时调用titleForHeaderviewForHeader。我将正确陈述规则,仅供记录:

The rule is simply that viewForHeaderwill not be called unless you somehow give the header a height. You can do this in any combination of three ways:

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

  • Implement tableView:heightForHeaderInSection:.

  • Set the table's sectionHeaderHeight.

  • Call titleForHeader(this somehow gives the header a default height if it doesn't otherwise have one).

  • 实施tableView:heightForHeaderInSection:

  • 设置表的sectionHeaderHeight.

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

If you do none of those things, you'll have no headers and viewForHeaderwon't be called. That's because without a height, the runtime won't know how to resize the view, so it doesn't bother to ask for one.

如果你什么都不做,你就没有标题,viewForHeader也不会被调用。这是因为如果没有高度,运行时将不知道如何调整视图的大小,所以它不会费心去要求一个。

回答by Sharukh Mastan

Giving estimatedSectionHeaderHeightand sectionHeaderHeightvalues fixed my problem. e.g., self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension

给予estimatedSectionHeaderHeightsectionHeaderHeight价值观解决了我的问题。例如, self.tableView.estimatedSectionHeaderHeight = 100 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension

回答by rrrrrraul

Going off rmaddy 's answer, I was trying to hide the Header view and was returning 0.0f for "tableView:heightForHeaderInSection" and a 0 height View from tableView:viewForHeaderInSection.

离开 rmaddy 的回答,我试图隐藏 Header 视图并为“tableView:heightForHeaderInSection”返回 0.0f 和从tableView:viewForHeaderInSection.

After changing from return 1.0fto return 0.0fin tableView:heightForHeaderInSection, the delegate method tableView:viewForHeaderInSectionwas indeed called.

return 1.0freturn 0.0fin 之后tableView:heightForHeaderInSection,委托方法tableView:viewForHeaderInSection确实被调用了。

Turns out my desired effect works without having to use "tableView:heightForHeaderInSection"; but this may be useful to others who are having an issue getting "tableView:heightForHeaderInSection" delegate method called.

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

回答by Benjohn

It's worth briefly noting that if your implementation of tableView:heightForHeaderInSection:returns UITableViewAutomaticDimension, then tableView:viewForHeaderInSection:will not be called.

值得注意的是,如果您执行了tableView:heightForHeaderInSection:return UITableViewAutomaticDimension,则tableView:viewForHeaderInSection:不会被调用。

UITableViewAutomaticDimensionassumes that a standard UITableViewHeaderFooterViewwill be used that is populated with the delegate method tableView:titleForHeaderInSection:.

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

From comments in the UITableView.h:

来自以下评论UITableView.h

Returning this value from tableView:heightForHeaderInSection:or tableView:heightForFooterInSection:results in a height that fits the value returned from tableView:titleForHeaderInSection:or tableView:titleForFooterInSection:if the title is not nil.

tableView:heightForHeaderInSection:或返回此值会tableView:heightForFooterInSection:导致高度适合从tableView:titleForHeaderInSection:tableView:titleForFooterInSection:如果标题不为零的返回值。

回答by Chiara

You should implement tableView:heightForHeaderInSection:and set the height for the header >0.

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

This delegate method goes along with the viewForHeaderInSection:method.

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

I hope this helps.

我希望这有帮助。

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

回答by Benjohn

I've just had an issue with headers not showing for iOS 7.1, but working fine with later releases I have tested, explicitly with 8.1 and 8.4.

我刚刚遇到了标题未在iOS 7.1 中显示的问题,但在我测试过的更高版本中运行良好,明确使用 8.1 和 8.4。

For the exact same code, 7.1 was notcalling any of the section header delegate methods at all, including: tableView:heightForHeaderInSection:and tableView:viewForHeaderInSection:.

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

After experimentation, I found that removingthis line from my viewDidLoadmade headers re-appear for 7.1 and did not impact other versions I tested:

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

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

… so, there seems to be some kind of conflict there for 7.1, at least.

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

回答by Najam

Same issue occured with me but as I was using automatic height calculationfrom xCode 9, I cannot give any explicit height value as mentioned above. After some experimentation I got solution, we have to override this method as,

同样的问题发生在我身边但是,因为我是用自动高度计算Xcode中9,如上面提到的,我不能给任何明确的高度值。经过一些实验,我得到了解决方案,我们必须重写这个方法,

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

Although I have checkedboth options

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

  1. Automatic Height Calculation
  2. Automatic Estimated Height Calculation
  1. 自动高度计算
  2. 自动估计高度计算

from storyboardas apple says, but still I got this weird error.

来自苹果所说的故事板,但我仍然遇到了这个奇怪的错误。

Please Note: This Error was shown only on IOS-10version not on IOS-11version. Maybe it's a bug from xCode. Thanks

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

回答by RanLearns

Here's what I've found (Swift 4) (thanks to this commenton another question)

这是我发现的(Swift 4)(感谢对另一个问题的评论

Whether I used titleForHeaderInSection or viewForHeaderInSection - it wasn't that they weren't getting called when the tableview was scrolled and new cells were being loaded, but any font choices I made for the headerView's textLabel were only appearing on what was initially visible on load, and not as the table was scrolled.

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

The fix was willDisplayHeaderView:

修复方法是 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)
    }
}