【问题标题】:Two different heights for sectionHeaderViewsectionHeaderView 的两种不同高度
【发布时间】:2014-11-16 03:15:21
【问题描述】:

我希望我的sectionHeaderView 在 iOS 应用程序中有两个不同的高度。我希望第一个 sectionHeaderView 是一个高度,而第二个(以及所有其余部分)是另一个高度。

注意:这不是表格标题视图,而是sectionHeaderView

- (CGFloat)tableView:(UITableView *)tableView heightForViewForHeaderInSection:     (NSIndexPath *)indexPath
    {
    if(indexPath.section == 0)
    {
        return 300;
    }
    return UITableViewAutomaticDimension;

    else {
    return 50;

  }
}

谢谢。

【问题讨论】:

  • 移除返回 UITableViewAutomaticDimension 并尝试
  • 这样的代码不会编译(语法错误)。 return 后有 else 语句。
  • 说真的,这更像是一个if-else 问题。与iOS无关。查看您的代码,这是错误的。首先,您的方法不是UITableViewDelegate 方法。也许它是故意的,并且您正在以某种方式使用它。这种情况下就好了。该表不会永远调用此方法。您的代码在语义上不正确,即使在语法上也不正确。您在 ifelse 块之间返回 UITableViewAutomaticDimension。当 if 计算结果为 true 时,它​​将返回 300。否则,它将返回 UITableViewAutomaticDimension。您的 else 案例将永远不会被调用。

标签: ios objective-c uitableview


【解决方案1】:

给你的小例子。

 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == First)
        {
            return 70;
        }

        return 45;
    }

【讨论】:

    【解决方案2】:

    你的代码错了试试这个:

     - (CGFloat)tableView:(UITableView *)tableView heightForViewForHeaderInSection:     (NSIndexPath *)indexPath
        {
            if(indexPath.section == 0) // First section
            {
                return 300;
            } else if(indexPath.section == 1) { // Second
                return 50;
            }
            else { // Any other section
                return 40.0; // Default
            }
      }
    

    【讨论】:

    • 看起来应该可以,但没有改变高度。我有一个sectionHeadern.nib。这会覆盖高度吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多