代码之家  ›  专栏  ›  技术社区  ›  Hunter

UITableView不考虑页眉部分的高度/页脚部分的高度?

  •  22
  • Hunter  · 技术社区  · 17 年前

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

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

    下面是它的样子-你可以看到顶部有大约20p的灰色空间,对于一个0行的部分,页眉和页脚各约10p。

    alt text
    (来源: hanchorllc.com )

    以下是我的伪代码:

    - (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的节页眉和页脚值设置为10.0(默认值),尽管我认为如果实现,委托方法会覆盖该值。

    这是一款面向3.0的应用程序。

    我做错了什么?

    9 回复  |  直到 7 年前
        1
  •  35
  •   Juris V    14 年前

    这有点棘手,因为不接受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
    }
    
        2
  •  26
  •   h4xxr    17 年前

    在iPhone上的“分组”UITableView中,它仍然会呈现页眉和页脚的最小高度,实际上会忽略将其设置为零的代码。它未链接到XIB默认值。

    这是因为零高度的节页眉或页脚看起来非常奇怪。因此,苹果已颁布法令,收割台高度不能设置为0。因此,根据您的屏幕截图,多个“空”部分会呈现出奇怪的效果。

    我担心这一切都是因为在没有数据行的情况下,清除头大小的方法是错误的;相反,你应该 不能为空节调用任何方法

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

    这个 实施的方式是:

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

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

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

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

    这个 对的 实施的方式是:

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

    希望有帮助!

        3
  •  9
  •   Jeff Mascia    15 年前

    这张桌子看起来很漂亮 tableView:heightForHeaderInSection: 只有 tableView:viewForHeaderInSection: 不是 nil ,或 tableView:titleForHeaderInSection: 不是 @"" . 对于页脚高度,情况也是如此。

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

    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
      return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    }
    
        4
  •  5
  •   ricardopereira    9 年前

    sectionFooterHeight 0 ,即在 viewDidLoad 添加:

    tableView.sectionFooterHeight = 0
    

    2) 添加委托方法:

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

    (使用Swift 2.2)

        5
  •  3
  •   Davidm    16 年前

    我也有类似的问题: 我正在加载一个 UITableView tableView:heightForFooterInSection (与您相同),但该值已被忽略。

    解决方法很简单:在XIB中也将页脚高度设置为0.0。(在Interface Builder中选择表格视图,按Command-3查看尺寸检查器,查找顶部附近的页脚高度字段)

        6
  •  1
  •   pkamb santoshIOS    10 年前

    这对我很有用:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        tableView.sectionHeaderHeight = (section == 0 ? 10 : 0);
        return tableView.sectionHeaderHeight;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        tableView.sectionFooterHeight = (section == 0 ? 20 : 4);
        return tableView.sectionFooterHeight;
    }
    
        7
  •  0
  •   mark    15 年前

    我对这个问题的解决方案是断断续续地描述h4xxr是如何描述的。然而,我有一种稍微不同的方法来构建动态数量的节。

    首先,我定义了一个方法,该方法将遍历我的数据结构,并标记数据是否应显示在表中。该标志存储在一个数组中,以便我可以包含任意少或任意多的节。如果我们只关注每个部分的单元数量,我可以更好地描述这个实现:

    假设我的第一部分是姓名、姓氏和年龄的联系方式。我将用id“1”来定义这个部分(尽管在技术上,正如我稍后解释的,这可以是任何数字)。现在,如果我的方法确定此部分应该可见,我将值“1”推送到一个数组上。

    我想展示的下一部分是地址,可能有3-4行/单元格。如上所述,我的方法中的逻辑通过在数组上按say“2”来确定地址是否可见。

    现在我们可以返回数组的长度来定义所需的节数。

    switch(ourArray[indexPath.section]){
        case 1:
            <Return the number of rows for the contact details which we said would be 3>
            break;
        case 2:
            <Return the number of rows for the address details which we said would be 4>
            break;
        case 3:
            <Return another number for some section that is referenced by the id '3'>
            break; 
    }
    

    请注意,我已将案例3放入开关中。因为我们示例中的数组只包含值“1”和“2”,所以案例3永远不会匹配,因此会被忽略,除非我们决定通过将其推送到数组来添加/启用此案例。

    还要注意的是,在定义哪些部分的逻辑可见的方法中,可以通过在数组中的不同位置插入/推送这些部分来更改这些部分的顺序。

    我虔诚地使用上面的内容,因为它允许我将索引和节的构造分离开来。

    最后,在使用“reloadData”更新表之前,我将调用我的方法,该方法将构造数组,并提供正确的节id长度和顺序,以使我的tableview知道如何构建它自己。如果我的数据发生了变化或以某种方式被过滤,我将再次调用此方法并重建数组。

        8
  •  0
  •   dronpopdev    7 年前

    我使用以下解决方案:

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? CGFloat.leastNormalMagnitude : 8
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    

    结果:

    result