位置按钮到动态TableView底部

时间:2018-02-09 23:36:05

标签: ios swift xcode autolayout tableview

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

我有一个TopView to TableView约束集,但是如果我的数据更长,它在TableView底部和按钮之间没有相同的空间。

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

感谢您的帮助

enter image description here

2 个答案:

答案 0 :(得分:1)

1- set tableView高度约束为3000(任何高数字)

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

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

3-将其设置为表格

self.tableViewHeightConstraint.constant = currentHeight 

4- hook按钮的顶部到tableView的底部+20

答案 1 :(得分:0)

这就是结束这样做的原因。

成功获取数据后。

            // 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)