【问题标题】:Default height for section header in UITableViewUITableView 中节标题的默认高度
【发布时间】:2010-11-15 02:04:02
【问题描述】:

我想在我的 UITableView 中设置第一个标题的高度。对于其他标题,我希望它们保持默认高度。我可以在下面的代码中用什么值/常数代替“someDefaultHeight”?

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return kFirstHeaderHeight;

    return someDefaultHeight;
}

谢谢

【问题讨论】:

  • 为什么不尝试不同的值直到你满意为止?
  • @Daniel - 如果 Apple 决定更改默认行高值,那么我需要确保我的应用程序不会硬编码这个值(任意数量)。如果在某处声明,最好将这些信息从常量中提取出来。

标签: iphone cocoa-touch uitableview


【解决方案1】:

对于 swift 4.2 你应该返回 UITableView.automaticDimension

【讨论】:

    【解决方案2】:

    要获取默认高度,只需让super处理即可:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 0)
            return kFirstHeaderHeight;
    
        return [super tableView:tableView heightForHeaderInSection:section];
    }
    

    【讨论】:

    • [super tableView:tableView heightForHeaderInSection:section]; 为我返回 0,可能是因为我没有使用笔尖或情节提要。
    • 这仅适用于子类化 UITableViewController。
    【解决方案3】:

    为了完整起见:在 iOS7+ 中,分组样式部分标题的高度是 55.5 用于第一个标题,38 用于后续标题。 (使用 DCIntrospect 测量)

    【讨论】:

      【解决方案4】:

      从 IOS 5.0 开始,您可以在大多数委托方法中返回 UITableViewAutomaticDimension。它位于文档页面的底部

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          if(section == CUSTOM_SECTION)
          {
              return CUSTOM_VALUE;
          }
          return UITableViewAutomaticDimension;
      }
      

      【讨论】:

      • hmm.. 至于我 UITableViewAutomaticDimension 返回 -1(硬编码 const),我在 UITableView 中根本看不到任何部分。
      • 为什么UITableViewAutomaticDimensionNSLog 时显示-1?
      • 这仅在您使用时有效:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 如果您正在实施- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 方法,则此方法不起作用。
      • 值得注意的是,如果你实现了这个和估计委托方法并返回UITableViewAutomaticDimension,它将有一个零高度。
      • @SuperSaiyen - 它确实适用于viewForHeaderInSection,您只需设置estimatedSectionHeaderHeight
      【解决方案5】:

      通过检查我的应用程序中的默认值,分组表的默认高度为 22,非分组表的默认高度为 10。

      如果您检查 tableview 上的属性 sectionHeaderHeight 的值,应该会告诉您。

      【讨论】:

      • 谢谢...我会硬编码到这个。虽然我确实希望这个值有一个常数。
      • 你有这些倒退。 UITableViewStyleGrouped 是 22,UITableViewStylePlain 是 10。
      【解决方案6】:

      我不确定这里的正确答案是什么,但 10 或 22 似乎都不是 iOS 5 中分组表格视图的正确高度。我使用的是 44,基于this 问题,它至少看起来大致正确的高度。

      【讨论】:

        【解决方案7】:

        实际操作:)

        - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
        {
            if(section == 0)
                return kFirstSectionHeaderHeight;
            return [self sectionHeaderHeight];
        }
        

        【讨论】:

        • 我认为您的意思是return [self.tableView sectionHeaderHeight];,或者更好的是return [tableView sectionHeaderHeight];。但是,两者都为我返回 -1,可能是因为我没有使用笔尖或情节提要。
        【解决方案8】:

        这应该可以解决问题

        - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if(indexPath.section == CUSTOM_SECTION)
            {
                return CUSTOM_VALUE;
            }
            return [tableView rowHeight];
        }
        

        【讨论】:

        • 我想你的意思是return [self sectionHeaderHeight];
        • @TMB [self sectionHeaderHeight]; 产生错误。你的意思是[tableView sectionHeaderHeight];
        猜你喜欢
        • 2010-10-14
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多