UITableView不尊重heightForHeaderInSection/heightForFooterInSection?

Hun*_*ter 23 iphone cocoa-touch

我有一个UITableView,在某些情况下,某些部分有零行.我的目标是,当这是真的,我不希望在表视图中浪费任何空间,它看起来应该没有数据.

我遇到的问题是这些部分的页眉和页脚,即使没有行也会显示,尽管我重写了委托方法以返回0.0f.

这是它的样子 - 你可以在那里看到顶部约20p的灰色空间,每个约10p的页眉和页脚用于0行的部分.

替代文字http://www.hanchorllc.com/table_cells.png

这是我的伪代码:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     if ([section hasRow]) {
          return 10.0f;
     } else {
          return 0.0f;
     }
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     if ([section hasRow]) {
          return 10.0f;
     } else {
          return 0.0f;
     }
}
Run Code Online (Sandbox Code Playgroud)

我已经验证了这些方法正在被调用,并且正在执行正确的执行路径.

一个皱纹 - 这个视图控制器正在使用XIB,并且UITableView的节头和页脚值设置为10.0(默认值),尽管我认为它被委托方法覆盖,如果实现的话.

这是一款定位于3.0的应用.

我究竟做错了什么?

小智 34

这有点棘手,因为不接受0.0f值.但任何接近于零的东西都可以解决问题.如果您决定不是像素完美并且想要圆形数字,那么1.0f将几乎相同,因为1px的高度差异将是相当明显的.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1 )
        return 0.000001f;
    else return 44.0f; // put 22 in case of plain one..
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001f; //removing section footers
}
Run Code Online (Sandbox Code Playgroud)


h4x*_*xxr 25

在iPhone上的"分组"UITableView中,它仍然会为页眉和页脚呈现最小高度,有效地忽略您的代码将其设置为零.它没有链接到XIB默认值.

这是因为零高度部分页眉或页脚看起来很奇怪.因此Apple已经下令不能将标题高度设置为0.因此,根据您的屏幕截图,多个"空"部分将呈现奇怪的结果.

我担心这一切都是因为当没有数据行时你清除标题大小是错误的; 相反,你不应该为空部分调用任何方法.这是一个糟糕的技术,因为基本上iPhone必须调用比它应该更多的方法,并且你渲染的标题比你想要的多(通常 - 有时候人们想要留下那里的空白标题,例如用于拖放) .

因此,例如,让我们假设您有许多部分,但只有部分部分具有多个部分(可能基于用户设置/过滤器).

实现它的错误方法是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return totalNumberOfPossibleSections;
}
Run Code Online (Sandbox Code Playgroud)

然后对于每个部分,您没有结果:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (![section hasRow]) return 0;
}
Run Code Online (Sandbox Code Playgroud)

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  if (![section hasRow]) return 0.0f;
}
Run Code Online (Sandbox Code Playgroud)

实现它的正确方法是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return numberOfSectionsWhichHaveAtLeastOneRowInThem;
}
Run Code Online (Sandbox Code Playgroud)

然后每个部分至少会有一个结果.这样,甚至没有渲染没有数据的部分,甚至没有为它们调用方法. 注意:我的两个变量名称用于表示它们包含的内容!它们不是特殊的Apple变量......

希望有所帮助!


Jef*_*cia 9

表格似乎tableView:heightForHeaderInSection:只有在tableView:viewForHeaderInSection:不是nil或者tableView:titleForHeaderInSection:不是nil或是的情况下才会尊重@"".对于页脚高度也是如此.

因此,假设您没有任何节标题视图或标题,只需将其添加到您的表委托:

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
Run Code Online (Sandbox Code Playgroud)


Ali*_*per 5

我发现在我的情况下需要两件事来杀死幻影页脚:

1)将表视图设置sectionFooterHeight0,即viewDidLoad添加:

tableView.sectionFooterHeight = 0
Run Code Online (Sandbox Code Playgroud)

2)添加委托方法:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView(frame: CGRect.zero)
}
Run Code Online (Sandbox Code Playgroud)

(使用 Swift 2.2)

  • 添加 'tableView.sectionFooterHeight = 0' 对我有用..谢谢 (2认同)