【问题标题】:Position button to dynamic TableView bottom将按钮定位到动态 TableView 底部
【发布时间】:2018-02-09 23:36:05
【问题描述】:

我有一个动态的 TableView,它会根据 API 内容改变大小。这工作正常,但我无法在 TableView 之外获得按钮或下面的任何内容以正确定位并保持空间一致。按钮根据 TableView 中的行数上下移动。

我有一个 Top Space to TableView 约束集,但如果我的数据较长,它不会在 TableView 底部和按钮之间提供相同数量的空间。

有没有办法将它锚定到 TableView 的底部,无论标题是 1 行还是 2 行等?

感谢您的帮助

【问题讨论】:

  • 这意味着如果许多单元格的标题很长,则按钮可能位于屏幕下方的底部???
  • 我正在截断它们,以免它们变得太长。但就像在带有 2 行标题的照片中一样,按钮向上。
  • 你在IB中给表格设置了静态高度吗
  • 然后使按钮顶部 = self.notesTable.contentSize.height + tabletopSpace
  • 问题是可见单元格的 self.notesTable.contentSize.height 值不是所有单元格的总和

标签: ios swift xcode autolayout tableview


【解决方案1】:

1- 将 tableView 高度约束设置为 3000(任何高数)

2- 获取最后一个单元格的边界并访问它的 y ,高度来自 bounds 属性

let currentHeight  =  y of last cell  + it's height 

3- 将其设置为表格

self.tableViewHeightConstraint.constant = currentHeight 

4- 钩子按钮顶部到 tableView 底部 + 20

【讨论】:

    【解决方案2】:

    这就是最终的做法。

    成功获取数据后。

                // Get visible cells and sum up their heights
                let cells = self.notesTable.visibleCells
                var heightOfTableView = 0
                for cell in cells {
                    heightOfTableView += Int(cell.frame.height)
                }
                //total tableview height after data loaded
                //so rows aren't cut off or blank
                self.tableViewHeightConstraint.constant = CGFloat(heightOfTableView)
                //distance from top of TableView to place button
                self.buttonFromTableTopConstraint.constant = CGFloat(heightOfTableView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多