【问题标题】:How to remove the blank space at the top of a grouped UITableView?如何删除分组 UITableView 顶部的空格?
【发布时间】:2012-04-13 12:47:14
【问题描述】:

当您使用UITableViewStyleGrouped 样式创建UITableView 时,它会在实际的tableviewcells 和表格框架的边框之间添加相当多的空间。此空间位于 tableviewcells 的上方、下方、左侧和右侧。

您可以通过以下方式更改 tableviewcells 之间的空间:

[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];

但是,即使这样做,框架顶部和第一个 tableviewcell 之间仍然存在这个烦人的空间。此外,在框架的左侧和 tableviewcell 之间以及框架的右侧和 tableviewcell 之间仍有大量空间。

-=-=-=-=-=-=-

有没有办法操纵那个空间(调整它的大小)?到目前为止,我唯一的解决方案是使框架的大小大于屏幕,以使程序在屏幕外具有“空白空间”,从而将其删除。然而,这显然不是最优的,也是一个明显的 hack。

【问题讨论】:

  • 为了实现这样的自定义行为,我建议正确的做法:UICollectionView

标签: objective-c uitableview


【解决方案1】:

这个答案来得很晚,但我希望它对某人有所帮助。

空间存在是因为UITableViewtableHeaderView 属性。当tableHeaderView 属性为nil 时,Apple 默认为一个视图。所以解决这个问题的方法是创建一个高度大于0 的空视图。设置此项会覆盖默认视图,从而删除不需要的空间。

这可以在 Storyboard 中通过将视图拖到 tableView 的顶部然后将视图的高度设置为 1 或更大的值来完成。

或者可以使用以下代码以编程方式完成:

目标-C:

CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];

斯威夫特:

var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)

评论

正如其他人所指出的,您可以对页脚使用相同的解决方案。


来源和致谢

有关tableHeaderView 属性的更多详细信息,请参阅Documentation

感谢@liushuaikobe 验证使用最小正正态数是否有效。

【讨论】:

  • 在阅读了关于滚动视图插图和默认 contentEdgeInsets 等的大约 10 个其他答案之后,这似乎是嵌入在 UIViewController 中的分组样式的最佳解决方案。
  • 一条线解决方案self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)]
  • @liushuaikobe:CGFLOAT_MIN 有用吗?当我写答案时,我似乎记得尝试过这个没有成功。
  • 如果你调用 heightForHeaderInSection 不要返回零,否则上面的代码将不起作用
  • 这是对的一半(至少对我来说)。额外空间的原因是正确的,但解决方案对我不起作用,所以我必须创建并注册单独的页眉和页脚类,然后将它们的高度设置为零以从分组的 uitableview 中删除额外的空间。跨度>
【解决方案2】:

单线解决方案:

目标-C

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];

斯威夫特

self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double(FLT_MIN)))

【讨论】:

    【解决方案3】:

    如果你的tableView的样式是UITableViewStyleGrouped,那么你要注意SectionHeader或者SectionFooter高度的delegate,因为这个需要在这种情况下实现。

    返回值不应该为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;
    } 
    

    确保你实现了这两个方法,并且值是正确的,并且上边距是固定的。

    【讨论】:

      【解决方案4】:

      viewDidLoad() 方法中使用它。

      tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))
      

      【讨论】:

      • 您可以简单地使用:tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: .leastNormalMagnitude))。无需显式转换为 Double。
      【解决方案5】:

      你也需要设置页脚

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

      【讨论】:

        【解决方案6】:

        适用于 iOS 11.0+

        tableView.contentInsetAdjustmentBehavior = .never
        

        【讨论】:

          【解决方案7】:

          在 Swift 4 中回答

          如果在界面生成器中选择了表格视图并且在属性检查器中选择了“分组”样式,请在视图控制器中输入以下代码以修复额外的标题空间问题。

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

          【讨论】:

          • 它解决了分组表视图顶部的空白空间。谢谢!!
          【解决方案8】:
          func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
              return CGFloat.leastNonzeroMagnitude
          }
          

          位置图标下方是顶部间距为零的表格。

          【讨论】:

            【解决方案9】:

            我必须将两个委托函数结合起来才能工作:

            func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                return .leastNonzeroMagnitude
            }
            
            func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                return UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: .leastNonzeroMagnitude))
            }
            

            【讨论】:

              【解决方案10】:

              以上解决方案都不适合我

              在我的情况下,这些设置设置为手动,我只是更改为自动

              【讨论】:

                【解决方案11】:

                如果您使用带有 insetGroup 样式的 UITableView 的 TableHeaderView,您可以执行以下操作:

                class CustomTableHeaderView: UIView {
                    
                    init() {
                        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
                        --- do your header view setup here. ---
                    }
                    
                    required init?(coder: NSCoder) {
                        fatalError("init(coder:) has not been implemented")
                    }
                }
                

                【讨论】:

                  【解决方案12】:

                  按照这些步骤来节省您的一天。

                  从情节提要中选择分组属性。

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

                  如果您使用的是 ios 15,也请执行此操作 -

                  if #available(iOS 15.0, *){ 
                      self.tableViewSavedRecent.sectionHeaderTopPadding = 0.0 
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-05-29
                    • 1970-01-01
                    • 2019-01-11
                    • 1970-01-01
                    • 2017-10-25
                    相关资源
                    最近更新 更多