ios 按数字或行设置 TableView 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31760815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:06:06  来源:igfitidea点击:

Set TableView height by the number or rows

iosswiftuitableview

提问by Eliko

I have got TableViewin the MainstoryBoardand the number of rows is random every time. I want the height of the whole TableViewto be flexible - by that I mean, for example: if I got 4 rows in the TableViewand each TableViewrow height is 22 so the TableViewheight will be 88. Another example: number of rows: 2 row height = 22 TableViewwill be 44.

我有TableViewMainstoryBoard每一次的行数是随机的。我希望整体的高度TableView是灵活的 - 我的意思是,例如:如果我有 4 行TableView,每TableView行高度为 22,那么TableView高度将为 88。另一个例子:行数:2 行高 = 22 TableView将是 44。

How can I make it?

我怎样才能做到?

回答by Sohil R. Memon

You can change the UITableView height as per the contentSize as below:

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

Swift 2.2

斯威夫特 2.2

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

Swift 3 or 4+

斯威夫特 3 或 4+

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

and make sure you write the above line in viewDidAppearmethod

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

You need to write the above line in viewDidLayoutSubviewsalso.

你还需要写上上面的行viewDidLayoutSubviews

Swift 2.2

斯威夫特 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+

斯威夫特 3 或 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()
}

回答by Mr. JD Agrawal

Take outlet of the Height Constraint of the parent view and assign it the height of table view's content + constant(extra height of other contents in the view)

取父视图高度约束的出口,并为其分配表格视图内容的高度+常量(视图中其他内容的额外高度)

heightConstraintView.constant = tableView.contentSize.height + constantValue //use the value of constant as required.

and write this code in the cellForRowAt method.

并在 cellForRowAt 方法中编写此代码。

回答by iOS

DispatchQueue.main.async {
    var frame = tableView.frame
    frame.size.height = tableView.contentSize.height
    tableView.frame = frame
}

OR

或者

DispatchQueue.main.async {
    self.TblViewHeightConstraint.constant = CGFloat((self.array.count) * 30)//Here 30 is my cell height
    self.TblView.reloadData()
}

回答by iOS

Simply take outlet for tableview height constraint and update it in willDisplay cell: UITableViewCellmethod of UITableView

只需为 tableview 高度约束取出口并以willDisplay cell: UITableViewCell方法更新它UITableView

@IBOutlet weak var tblHConstraint: NSLayoutConstraint!


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        self.tblHConstraint.constant = self.tblView.contentSize.height
    }

回答by joemissamore

To use with with autolayout:

与自动布局一起使用:

Somewhere in viewDidLoad()

在某处 viewDidLoad()

tableView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor)

and then in your viewDidLayoutSubViews()

然后在你的 viewDidLayoutSubViews()

tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true

回答by suresh

Note:when your are increasing the tableview height it will goes out side the view. and you will get a problem with scrolling.

注意:当您增加 tableview 高度时,它将超出视图。你会遇到滚动问题。

Ex: take a view inside that keep tableview and give constraints to left,right,bottom,top to Zero(0).

例如:在内部查看保持 tableview 的视图,并将对左、右、底部、顶部的约束设置为零(0)。

now reload the tableview assume 2 rows each row height is 20 now total rows height is 40. its fine and gave those height to table view, Table view will show only 40 height (you can assign tableview height by taking constraints (or) table view content size both are give same height according to rows height).

现在重新加载 tableview 假设 2 行每行高为 20 现在总行高为 40。它很好并将这些高度提供给 table view,Table view 将仅显示 40 高度(您可以通过约束(或)table view 分配 tableview 高度内容大小都根据行高给出相同的高度)。

But if you reload 50 rows you will get scrolling problem because those rows height given to table view height, here table view height expands more than your screen height.

但是如果你重新加载 50 行,你会遇到滚动问题,因为这些行高给了表格视图高度,这里表格视图高度扩展到超过你的屏幕高度。

To solve this problem you should use scroll view outside the tableview.

要解决此问题,您应该在 tableview 之外使用滚动视图。

回答by Sreeraj VR

Use this for Swift 3

将此用于 Swift 3

 override 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()
}