gpt4 book ai didi

ios - 按行数或行数设置 TableView 高度

转载 作者:IT王子 更新时间:2023-10-29 05:16:47 24 4
gpt4 key购买 nike

我有TableViewMainstoryBoard每次行数都是随机的。我想要整体的高度TableView灵活——我的意思是,例如:如果我在 TableView 中有 4 行每个 TableView行高为 22,所以 TableView高度将是88。另一个例子:行数:2行高 = 22 TableView将是 44。

我怎样才能做到?

最佳答案

您可以根据 contentSize 更改 UITableView 的高度,如下所示:

Swift 2.2

tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)

Swift 3 or 4+

tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)

并确保在 viewDidAppear 方法中编写上面的行

你还需要在 viewDidLayoutSubviews 中写上一行。

Swift 2.2

func viewDidLayoutSubviews(){
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height)
tableView.reloadData()
}

Swift 3 or 4+

func viewDidLayoutSubviews(){
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
tableView.reloadData()
}

关于ios - 按行数或行数设置 TableView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31760815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com