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

inf*_*eqd 120 uitableview ios uitableviewsectionheader

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

为什么会这样?

rma*_*ddy 248

使用tableView:viewForHeaderInSection:要求您也实施tableView:heightForHeaderInSection:.这应该为标题返回适当的非零高度.还要确保你没有实现tableView:titleForHeaderInSection:.您应该只使用一个或另一个(viewForHeadertitleForHeader).

  • 你绝对可以拥有两者.viewForHeaderInSection:将优先于titleForHeaderInSection:唯一的要求是在表视图上使用不同于0的东西设置estimatedSectionHeaderHeight,否则将永远不会调用viewForHeaderInSection: (4认同)
  • 确保方法签名中没有拼写错误.一封错误的字母意味着它不会被调用.检查案例.还要确保从`numberOfSections`返回0. (3认同)

Yun*_*hel 38

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

这意味着:

  1. 如果要实现的方法,但指定自己只为dataSourceUITableView,tableView:viewForHeaderInSection实现你的将被忽略,不会被调用.

  2. tableView:viewForHeaderInSection有更高的优先级.如果你同时实现的方法和分配自己作为这两个dataSourcedelegateUITableView,您将返回意见的部分信息,但是你tableView:titleForHeaderInSection:会被忽略.

  3. 出于研究目的,我删除了我的tableView:heightForHeaderInSection:; 它工作正常,似乎没有影响上述程序.但评论文件指出,tableView:viewForHeaderInSection正确运作是必要的; 为了安全起见,实施这一点是明智之举.

  • 你让我今天一整天都感觉很好!!!忘了将``UITableViewDelegate``分配给``self``,因为我认为``tableView:viewForHeaderInSection``是一个``UITableViewDataSource``方法.谢谢! (4认同)

mat*_*att 27

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

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

  • 实施tableView:heightForHeaderInSection:.

  • 设置表格sectionHeaderHeight.

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

如果你不做这些事情,你将没有标题,viewForHeader也不会被调用.那是因为没有高度,运行时将不知道如何调整视图的大小,因此它不需要请求一个.

  • @texas不,我不做xamarin.在Cocoa框架之上添加另一级别的间接性只会让我的头脑爆炸.:) (2认同)

Sha*_*tan 20

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


rrr*_*aul 7

关于rmaddy的回答,我试图隐藏Header视图并返回0.0f表示"tableView:heightForHeaderInSection"和0高度View tableView:viewForHeaderInSection.

从更改return 1.0freturn 0.0fin后tableView:heightForHeaderInSection,tableView:viewForHeaderInSection确实调用了委托方法.

无需使用"tableView:heightForHeaderInSection"即可实现我想要的效果.但是对于那些在调用"tableView:heightForHeaderInSection"委托方法时遇到问题的人来说,这可能会有用.


Chi*_*ara 5

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

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

我希望这有帮助。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
         return 40;
}
Run Code Online (Sandbox Code Playgroud)


Ben*_*ohn 5

值得一提的是,如果您执行tableView:heightForHeaderInSection:return UITableViewAutomaticDimension,那么tableView:viewForHeaderInSection:将不会被调用。

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

来自的评论UITableView.h

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