简体   繁体   English

将按钮定位到动态TableView底部

[英]Position button to dynamic TableView bottom

I have a dynamic TableView that changes size based on API content. 我有一个动态的TableView,可以根据API内容更改大小。 That is working fine, but I can't get a button or any content below, outside of the TableView to position correctly and keep the space consistent. 效果很好,但是在TableView之外,我无法获得按钮或下面的任何内容来正确定位并保持空间一致。 The button moves up and down depending on the number of rows in the TableView. 该按钮根据TableView中的行数上下移动。

I have a Top Space to TableView constraint set, but it doesn't give me the same amount of space between the bottom of the TableView and the button if my data is longer. 我有一个“顶部空间到TableView”约束集,但是如果我的数据较长,它不会在TableView底部和按钮之间提供相同的空间。

Is there any way to anchor that to bottom of the TableView regardless of if the heading has 1 or 2 lines, etc? 有什么方法可以将其锚定到TableView的底部,而不管标题是1行还是2行,等等?

Thanks for any help 谢谢你的帮助

在此处输入图片说明

1- set tableView height constraint to 3000 (any high number) 1-将tableView高度限制设置为3000(任何高数)

2- get bounds of the last cell and access it's y , height from bounds property 2-获取最后一个单元格的边界并从bounds属性访问它的y,height

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

3- set it to table 3-将其设置为表格

self.tableViewHeightConstraint.constant = currentHeight 

4- hook button's top to tableView's bottom + 20 4-挂钩按钮的顶部到表格视图的底部+ 20

This is how ended up doing it. 这是最终完成的方式。

After successfully getting the data. 成功获取数据后。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM