【问题标题】:Reducing the space between sections of the UITableView减少 UITableView 部分之间的空间
【发布时间】:2011-02-18 12:37:59
【问题描述】:

有没有办法减少 UITableView 的两个部分之间的空间?我拥有的每个部分之间大约有 15 个像素。我已经尝试为 -tableView:heightForFooterInSection:-tableView:heightForHeaderInSection: 返回 0 但这并没有改变任何东西。

有什么建议吗?

【问题讨论】:

  • 我使用 grouped tableView 并将页眉/页脚高度设置为0.0。但它显示了一个(默认)高度为 30 点的灰色区域。不接受使用0.0。您必须使用高于0.0 的任何值,例如0.0001.
  • 正如@Honey 所说,0.0 不起作用。对于同样的问题,我在这里有一个更彻底的答案:stackoverflow.com/a/22185534/2789144

标签: iphone ios uitableview


【解决方案1】:

这有点棘手,但试试这个代码:

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

    return 1.0;
}

- (CGFloat)tableView:(UITableView*)tableView 
           heightForFooterInSection:(NSInteger)section {
    return 5.0;
}

- (UIView*)tableView:(UITableView*)tableView 
           viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView*)tableView:(UITableView*)tableView 
           viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

相应地更改值。去掉空格,我觉得 0.0 是不会被接受的。最小的健全值似乎是 1.0。

【讨论】:

  • 你应该返回 5.0f 而不是 5.0,因为双浮点冲突
  • 您甚至可以返回 0.00001f 作为高度,您将获得 0 像素/点的高度。
  • 正如@Klass 指出的那样,您不能使用0 作为高度——您将获得默认高度。
  • 为什么不使用 CGFLOAT_MIN?它是为这些场景设计的:)
  • 如果使用swift,可以返回CGFloat.leastNonzeroMagnitude
【解决方案2】:

对于所有想要将距离缩小到 0 的人,您必须使用:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

因为 UITableViewDelegate 的服务只会从大于零的浮点数开始产生效果。

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1.0;
}


-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0;
}

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

-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

(使用 iOS 4.1 和 XCode 4.0.2)

【讨论】:

  • 返回 0.0001;正在为我工​​作。我正在使用 xcode 4.6 和 iOS 6.1
  • 我只需要在前两种方法中返回 CGFLOAT_MIN 就可以了。
  • 而不是 0 使用 return .leastNormalMagnitude
【解决方案3】:

您实际上可以在 Interface Builder 中的 size 选项卡下设置页脚/页眉/单元格的高度。默认情况下,页眉/页脚设置为 10.0。

【讨论】:

  • 我认为这种行为是在 iOS 5.0 或 iOS 6.0 中添加的,但是是的 - 现在设置组之间的距离要容易得多。
  • 我知道这很旧,但我想评论一下,从 XCode 5 和 iOS 7 开始,这似乎必须在代码中完成。我在界面生成器中将它设置为 0,但它仍然设置为 1,直到我在代码中将其设置为 0。
  • 对于 Xcode 6 和 iOS 8,它似乎只使用情节提要中的配置。
  • 默认情况下它们在 xcode 7 上设置为 18。但这是迄今为止最简单的解决方案!
【解决方案4】:

您必须降低部分页眉/页脚高度。那么节之间的空间就会减少。

试试这个代码

它对我有用:-)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;

【讨论】:

    【解决方案5】:

    您还可以从情节提要中降低节页脚和页眉的高度。在 tableview -> size 检查器中。转到剖面高度。

    默认情况下,普通样式表设置为 22,分组样式表设置为 10。您可以通过分别增加/减少页眉和页脚的值来配置值。

    【讨论】:

      【解决方案6】:

      对我来说,问题是因为我使用了分组表视图样式 (UITableViewStyleGrouped)。当我将其更改为普通样式 (UITableViewStylePlain) 时,我的 tableView:heightForHeaderInSection: 方法是唯一确定每个节标题大小的方法。

      【讨论】:

        【解决方案7】:

        也许用这个,0 不会按预期工作

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

        【讨论】:

          【解决方案8】:

          iOS7 更新:由于 ARC 自动发布更新,这里是 @Martin Stolz 代码编辑。

          -(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
          {
              if(section == 0)
                  return 6;
              return 1.0;
          }
          
          -(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
          {
              return 5.0;
          }
          
          -(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
          {
              return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
          }
          
          -(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
          {
              return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
          }
          

          (使用iOS7,Xcode v5.0)

          【讨论】:

            【解决方案9】:

            对我来说,只需使用以下代码即可解决此问题,

            func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                return section == 0 ? 1 : 20 // 20 is my other sections height
            }
            

            为第一部分返回 1 已解决问题。

            【讨论】:

              【解决方案10】:

              要更改页眉/页脚空间,必须实现以下方法:

              func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
              

              func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
              

              (使用相应的方法改变页脚高度)

              以下代码完全删除了部分周围的空格:

              public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                  return nil
              }
              
              public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                  return nil
              }
              
              public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                  return .leastNonzeroMagnitude
              }
              
              public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                  return .leastNonzeroMagnitude
              }
              

              【讨论】:

                猜你喜欢
                • 2012-05-29
                • 2015-08-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-06-17
                相关资源
                最近更新 更多