【问题标题】:Space Between Sections in UITableviewUITableview中部分之间的空间
【发布时间】:2015-08-02 13:12:30
【问题描述】:

我需要减少UITableView 的两个部分之间的空间。我查看了这个question,但该解决方案不允许我的自定义页眉视图,因为它结合了页脚和页眉的空间。

这是UITableView 的图片。黑色是UITableView 背景色。

【问题讨论】:

  • 我对你想要做什么感到困惑。是否要删除一节末尾和下一节标题之间的空白?
  • 是的,我想删除黑色空间
  • Xcode 6.0 及更高版本仅需要self.tableV.sectionHeaderHeight = 0.0; self.tableV.sectionFooterHeight = 0.0;
  • 你得到你想要的了吗? Bcz 我没有任何成功
  • 今天遇到了类似的问题,这救了我的命>stackoverflow.com/a/19296414/992461

标签: ios uitableview swift layout


【解决方案1】:

您是否尝试过覆盖此功能:

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

【讨论】:

  • 是的,它不起作用我认为该函数的最小值是 1
  • 我注意到你不能在 heightForFooterInSection 中返回 0,尝试返回 0.00001f 作为高度,你会得到 0 像素/点的高度。
  • 对于 Swift 3,您可以返回 CGFloat.leastNormalMagnitude。谢谢伊卡罗!
  • 这仅在您还覆盖 viewForFooterInSection 方法时才有效。这种方法你可能会返回nil(至少在iOS14中。我在以前的版本中没有尝试过)。
  • 至少对于 iOS 13,这仅适用于 @Mig70 评论
【解决方案2】:

在 iOS 15 上,您可能希望减少 sectionHeaderTopPadding

if #available(iOS 15.0, *) {
    tableView.sectionHeaderTopPadding = 0
}

【讨论】:

  • 感谢您的解决方案。节省时间:)
  • 救了我的命!非常感谢?
  • 谢谢 这在 iOS 15.0 中对我有用...但是此代码禁用了显示最新部分的默认功能。它与带有行的普通表格视图一样工作。
  • 非常感谢。苹果默认引入非零填充的方式真是太糟糕了。史诗
  • 有什么方法可以应用到应用程序中的所有表格视图,而不必为每个表格视图设置?
【解决方案3】:

我认为您可以通过将页脚视图高度调整为其最小值来解决此问题:在 Storyboard 或 XIB 中。

我不知道您在代码中为页脚高度写了什么。对不起,如果我错了。

Hide footer view in UITableView 可能重复

【讨论】:

  • 当我尝试最小页脚大小为 1 时仍然留下 1 点的差距
  • 您可以将该页脚的颜色更改为页眉部分的颜色.. 这样就可以忽略 1px 黑色。
  • @SudhinDavis 你是对的,xib 中有页脚高度,但如果我们不想要页脚,那么 Icaro 的回答 stackoverflow.com/a/30386480/2849443 是正确的
  • 那个答案比我的要好得多。因为这也会从空单元格中删除多余的行。谢谢。
  • 我喜欢stackoverflow.com/questions/11445301/…中给出的可能解决方案
【解决方案4】:

对于 Swift 4+ 你需要实现这两个方法

extension MyViewController : UITableViewDelegate {

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

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }

}

【讨论】:

  • 这些是 UITableViewDelegate 方法。你不要覆盖它们。
  • 好的,我将其编辑为我对更好和通用实施的回答。干杯。
  • 不错!不知道为什么这没有更多的赞成票。在viewForFooterInSection 中返回UIViewnil 很重要。只使用heightForFooterInSection(就像其他答案中提到的那样不起作用)
【解决方案5】:

对于 Swift 3

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

【讨论】:

  • 那是一个 UITableViewDelegate 方法。你不要覆盖它。
  • @DrewC 您确实在 Table View Controller 中覆盖它。
【解决方案6】:

除了answer posted by Icaro 我想补充一点,您还需要为要删除下面空白的部分实现返回niltableView:viewForFooterInSection: 方法 然后它会变成:

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

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

【讨论】:

    【解决方案7】:

    对于 Swift 5+:

    默认情况下页眉和页脚有一些空间。这就是为什么我在为这些部分设置精确分隔时遇到问题的原因。

    我将两个部分分开的解决方案如下:

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 24
        } else if section == 1 {
            return 32
        } else {
            return 40
        }
    }
        
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
        
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }
        
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        CGFloat.leastNormalMagnitude
    }
    

    正如你看到的 viewForFooterInSection 和 viewForHeaderInSection 我需要返回 nil。

    如果您只想更改 footerHeight,只需为 heightForHeaderInSection 返回 CGFloat.leastNormalMagnitude,并在 heightForFooterInSection 中返回每个部分的高度。

    【讨论】:

      【解决方案8】:

      您需要使用heightForHeaderInSection 方法来定义标题和单元格文本之间的空间。您也可以根据不同的部分进行更改,例如。在某些部分,您可能需要显示更多距离,而在某些部分,您不想显示间隙。对于这种情况,您可以使用CGFLOAT_MIN,即 0.000001f。举个例子,如何使用不同标题高度的不同部分:

      - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      {
          if (section == 0 || section == 2)
          {
              return 55.0;
          }
          else
          {
              return CGFLOAT_MIN;
          }
      }
      

      【讨论】:

      【解决方案9】:

      在情节提要/objectCode 中选择tableView,并确保将样式设置为Plain,而不是Grouped。您可以在属性“检查器”选项卡中找到此设置。

      let myTableView : UITableView = {
              let tableView = UITableView(frame: .zero, style: .plain)
              tableView.register(TableCellClass.self, forCellReuseIdentifier: "cellId")
              tableView.backgroundColor = UIColor(red: 123/255, green: 190/255, blue: 120/255, alpha: 1)
              tableView.separatorStyle = .none
              tableView.translatesAutoresizingMaskIntoConstraints = false
              return tableView
          }()
      

      【讨论】:

      • 请检查并在您的代码中验证 Style 设置为 plain 如果故事板您可以在 tableView(属性检查器)中找到这个词
      【解决方案10】:

      这也可能有所帮助:

      override func viewDidLoad() {
          super.viewDidLoad()
          tableView.sectionHeaderHeight = UITableViewAutomaticDimension
      }
      

      【讨论】:

        【解决方案11】:

        TableView 委托方法不影响浮点值为 0.0f。尝试给出一个更大的值。

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

        【讨论】:

          【解决方案12】:

          除了实现UITableViewDelegate 方法并通过CGFloat.leastNormalMagnitude 定义sectionFooterHeight 之外,还可以直接

          tableView.sectionFooterHeight = 0
          

          在没有页脚的情况下,节之间的间距将会消失。

          机制是默认这个值设置为UITableView.automaticDimension

          只要

          • 它保持UITableView.automaticDimension
          • 没有实现页脚配置的委托/数据源方法,即titleForFooterInSection/viewForFooterInSection
          • 表格视图的样式设置为.grouped

          然后 UITableView 会故意在没有视图的部分之间插入一个间距。

          你把sectionFooterHeight改成0,魔法就消失了。

          【讨论】:

            【解决方案13】:

            在 Xcode 13.2 中,您可以调整情节提要中部分的页眉和页脚的高度 - 请参见下面的屏幕截图:

            【讨论】:

              【解决方案14】:

              你可以通过实现委托heightForHeaderInSection & heightForFooterInSection来做到这一点。

              返回值不应为0,即使SectionHeader或SectionFooter的高度为0,也需要很小的值,试试CGFLOAT_MIN

              以我为例:

              - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
              if (section == [self.dataArray indexOfObject:self.bannerList]) {
                  return 46;
              }
              return CGFLOAT_MIN;
              

              }

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

              【讨论】:

                【解决方案15】:

                为我工作

                tableView.sectionFooterHeight = 10
                // ...
                
                func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                    return nil
                }
                

                【讨论】:

                  猜你喜欢
                  • 2011-02-18
                  • 1970-01-01
                  • 2012-05-29
                  • 1970-01-01
                  • 2017-04-17
                  • 2015-04-07
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多