【问题标题】:Add empty (blank) space under (in the end of) UITableView在 UITableView 下(末尾)添加空(空白)空间
【发布时间】:2016-08-16 10:53:43
【问题描述】:

我的应用程序有UITableView,其中包含可变数量的单元格。

如何在UITableView 下添加具有一定高度的空白(空白)空间?还是在UITableView 的最后?这样在最后一个单元格之后会有更多的空白空间。

【问题讨论】:

  • viewDidLoad中添加tbl_view.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

标签: ios objective-c uitableview


【解决方案1】:

在表格视图上设置内容插图:

var insets: UIEdgeInsets = tableView.contentInset
insets.bottom = 100
tableView.contentInset = insets

或者,简单地说:

tableView.contentInset.bottom = 100

滚动到最后一个单元格后,表格视图下会留下一些空白。

【讨论】:

  • 或者干脆self.tableView.contentInset.bottom = 100
  • @Siklab.ph ,你是整个页面上唯一知道如何做到这一点的人 :)
  • 这个答案确实不对。您正在更改其他插图,这几乎总是不是您想要做的。
  • 我真的很抱歉,但这是错了。它会破坏许多其他的东西。 Siklab.ph 给出了简单的正确答案。我提到这一点是为了让人们在谷歌上搜索这个 10,000 视图质量检查。
  • @Siklab.ph 我希望你能从我的投票中获得积分。感谢您的简单回答。
【解决方案2】:

说实话就是这么简单:

添加以下内容,可能在viewDidLoad

tableView.contentInset.bottom = 100

真的就是这样。

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.contentInset.bottom = 100
}

【讨论】:

  • 对于任何 Obj-C 恐龙 UIEdgeInsets insets = UIEdgeInsetsMake(0.0, 0.0, 100.0, 0.0); [self.tableView setContentInset:insets]; 顺序是“上、左、下、右”
  • 超级简单的解决方案,效果很好。谢谢。
【解决方案3】:

你可以使用 tableView Footer 来做这个

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)];
[self.tableView setTableFooterView:view];

【讨论】:

    【解决方案4】:

    感谢@Siklab.ph 为我提供最佳答案。

    self.tableView.contentInset.bottom = 100
    

    【讨论】:

    • 要明确的是,这是此页面上唯一正确的答案:/
    【解决方案5】:

    小心程序化滚动和底部空白空间定义

    需要考虑的一件事:如果您碰巧使用编程方式滚动到 tableView 中的某个单元格。然后你需要添加足够的底部空间来防止丑陋的scrolling-and-not-having-enough-bottom-space-causing-up-and-down-jumping-of-your-cells effect(也就是说,这完全取决于你的滚动代码——我只是想提一下)。

    因此,将额外空间设置为 cellHeight 的倍数实际上是一个好主意(如果以编程方式滚动,则添加正确的 cellHeight 倍数作为额外空间)...

    已经提到的解决方案比我的解决方案短。但是,如果您需要对底部空白进行额外的自定义,并且您不需要 tableView-footerView 来做其他事情,您也可以使用 footerView 来创建额外的底部空白和做:

    let rowHeight = 70  // or whatever...
    let nrOfCellHeightsSpaces = 6  // or whatever.. (consider scrolling !)
    let heightDependentSpace = nrOfCellHeightsSpaces * rowHeight
    let customViewFooter = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: heightDependentSpace))
    customViewFooter.backgroundColor = .clear  // or whatever color
    // add whatever customization of your extra bottom-space
    // (such as copyrights, decent logos, product information, etc, etc)
    tableView.tableFooterView = customViewFooter
    

    【讨论】:

      【解决方案6】:

      老实说,我不喜欢过多地弄乱插图(尤其是如果您已经出于其他原因修改了它们)。

      我找到的解决方案是覆盖tableView(_:heightForFooterInSection:) 方法并返回tableView 的最后一部分所需的空间高度:

      override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          if section == lastSectionIndex {
              return 30.0
          }
          return 0.001
      }
      

      最后一个0.001 值适用于所有其他部分,就我而言,它们没有任何页脚;这样,其他部分的页脚就不见了。 无需额外创建视图。

      【讨论】:

        【解决方案7】:

        我之前在使用footerView 和部分时遇到了一些问题,所以这是另一种对我有用的方法,您也可以尝试创建一个自定义cell,它只是一个emptyView 并显示最后一个索引处的那个特定的cell

        numberOfRowsInSection 中,只需将总行数设置为您要显示的总行数的 1。

        cellForRowAtIndexPath,你可以做一些类似的事情-

        if(indexPath.row == size + 1){
             //show custom cell
        }
        

        【讨论】:

        • 是的,不要这样做
        【解决方案8】:

        只需在情节提要的表格底部添加一个空白清晰视图即可。

        【讨论】:

          猜你喜欢
          • 2011-04-26
          • 2015-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-21
          • 1970-01-01
          • 2013-10-16
          • 2012-08-06
          相关资源
          最近更新 更多