UITableView不尊重heightForHeaderInSection / heightForFooterInSection? [英] UITableView Not Respecting heightForHeaderInSection/heightForFooterInSection?

查看:214
本文介绍了UITableView不尊重heightForHeaderInSection / heightForFooterInSection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



具有用于节的头部和尾部,即使没有行,尽管我重写委托方法返回0.0f。



这是它的外观喜欢 - 你可以看到顶部的〜20p的灰色空间,页眉和页脚大约10p每段为0行。





这里是我的伪代码:

   - (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;
}
}



我验证了这些方法被调用,



一个问题 - 这个视图控制器使用XIB,UITableView的section header和footer的值设置为10.0(默认)



这是一个针对3.0的应用程序。



解决方案

在iPhone上的分组UITableView中,它仍然会渲染标题的最小高度和页脚,有效地忽略您的代码将其设置为零。它未链接到XIB默认值。



这是因为零高度部分标题或页脚看起来很奇怪。因此,苹果已经宣布,标题高度不能设置为0.因此,多个空部分将呈现奇怪的截图。



恐怕都是因为当没有数据行时,您将错误地清除头大小;而应该不要调用任何空方法。这是一个坏的技术,因为本质上iPhone是不得不调用更多的方法,而不是你要渲染更多的头部,通常 - 有时人们想留下空白标题,例如拖放) 。



例如,假设您有多个节,但只有一些节有多行(可能基于用户设置/过滤器)。



错误的实施方式是:

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return totalNumberOfPossibleSections;
}

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

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(! 0;
}

   - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if(!
}

正确 / p>

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return numberOfSectionsWhichHaveAtLeastOneRowInThem;
}

,然后每个部分至少有一个结果。这样,没有数据的节甚至不被渲染,方法甚至不被调用。 注意:我的两个变量名称是为了表达它们将包含什么!它们不是特殊的苹果变量...



希望有所帮助!


I have a UITableView where in some instances, certain sections have zero rows. My goal is that when this is true, I don't want any wasted space in the table view, it should look like there's no data.

The problem I'm having is with the header and footer for the sections, which are showing even if there's no row and despite me overriding the delegate method to return 0.0f.

Here's what it looks like - you can see the ~20p of gray space at the top there, headers and footers of about 10p each for a section with 0 rows.

Here's my pseudo code:

- (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;
     }
}

I have verified that these methods are being called and that the proper execution path is taking place.

One wrinkle - this view controller is using a XIB and that UITableView has the section header and footer values set at 10.0 (default), though I thought that was overriden by the delegate method, if implemented.

This is an app targeting 3.0.

What am I doing wrong?

解决方案

In a "grouped" UITableView on the iPhone it will still render a minimum height for header and footer, effectively ignoring your code to set it to zero. It is not linked to the XIB default.

This is because a zero height section header or footer would look very odd. Therefore Apple has decreed that a header height cannot be set to 0. And therefore multiple "empty" sections will render oddly as per your screenshot.

I'm afraid it's all because you're going the wrong way about clearing the header size when there are no data rows; instead you should not be calling any methods for empty sections. It is a bad technique because essentially the iPhone is having to call more methods than it ought to, and also you render more headers than you want to (usually - sometimes people want to leave the blank headers there, for example for drag and drop).

So, for example, let's imagine that you have a number of sections, but only some of them have more than one row in (perhaps based on user settings/filters).

The wrong way of implementing it is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return totalNumberOfPossibleSections;
}

and then for each section you have no result for:

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

and

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

The correct way of implementing it is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return numberOfSectionsWhichHaveAtLeastOneRowInThem;
}

and then each section will have at least one result. That way, sections with no data aren't even rendered, methods aren't even called for them. Note: my two variable names are made up to convey what they'll contain! They're not special Apple variables...

Hope that helps!

这篇关于UITableView不尊重heightForHeaderInSection / heightForFooterInSection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆