ios 如何使用自动布局功能使 UITableView 的高度动态化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18498098/
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 00:53:51  来源:igfitidea点击:

How to make UITableView's height dynamic with autolayout feature?

iosuitableviewautolayout

提问by Burak

I am using autolayout in Xcode 5.

我在 Xcode 5 中使用自动布局。

I set the table view's height to Greater than or equal to 200px. I want that it has dynamic size. Because sometimes it will have many rows, sometimes it will have a few rows.

我将 table view 的高度设置为大于或等于 200px。我希望它具有动态大小。因为有时它会有很多行,有时它会有几行。

But the size is always 200px. And if the content is larger than that, I should scroll down to see the lower rows.

但大小总是 200px。如果内容大于那个,我应该向下滚动以查看较低的行。

What should I do to give the tableview dynamic size?enter image description here

我应该怎么做才能给 tableview 动态大小?在此处输入图片说明

回答by Juan Catalan

This is tested with the latest version of Xcode.

这是使用最新版本的 Xcode 进行测试的。

1) In Xcode go to the storyboard and click on you table view to select it.

1) 在 Xcode 中,转到故事板并单击您的表视图以选择它。

2) In the Utilities pane (see picture 1) make sure the constraint for the table view height is defined.

2) 在 Utilities 窗格中(见图 1),确保定义了表格视图高度的约束。

Table view heigth constraint

表视图高度约束

3) In the view controller that displays the table view create an outlet to this constraint:

3) 在显示表视图的视图控制器中,为此约束创建一个出口:

In Swift 3

在斯威夫特 3

@IBOutlet weak var dynamicTVHeight: NSLayoutConstraint!

In Objective C

在目标 C

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *dynamicTVHeight;

4) In the storyboard go back to the height constraint for the UITableViewand verify that the icon in the right has changed the color from purple to blue (see picture 2)

4) 在故事板中返回高度约束UITableView并验证右侧的图标是否已将颜色从紫色变为蓝色(见图 2)

Table view heigth constraint after outlet

出口后的表视图高度约束

4) Also put this code in the same view controller:

4)也把这段代码放在同一个视图控制器中:

Swift

迅速

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let height = min(self.view.bounds.size.height, tableView.contentSize.height)
    dynamicTVHeight.constant = height
    self.view.layoutIfNeeded()
}

Objective C

目标 C

- (void)viewWillAppear:(BOOL)animated
{
    // just add this line to the end of this method or create it if it does not exist
    [self.tableView reloadData]; 
}

-(void)viewDidLayoutSubviews
{
    CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);
    self.dynamicTVHeight.constant = height;
    [self.view layoutIfNeeded];
}

This should solve your problem.

这应该可以解决您的问题。

These are the links to two versions of the sample project that does what you want, one for Objective C and the other one for Swift 3. Download it and test it with Xcode. Both projects work with the latest version of Xcode, Xcode 8.3.2.

这些是示例项目的两个版本的链接,可以执行您想要的操作,一个用于 Objective C,另一个用于 Swift 3。下载它并使用 Xcode 进行测试。这两个项目都适用于最新版本的 Xcode,即 Xcode 8.3.2。

https://github.com/jcatalan007/TestTableviewAutolayouthttps://github.com/jcatalan007/TestTableviewAutolayoutSwift

https://github.com/jcatalan007/TestTableviewAutolayout https://github.com/jcatalan007/TestTableviewAutolayoutSwift

回答by Mohit Tomar

create your cell by xib or storyboard. give it's outlet's contents. now call it in CellForRowAtIndexPath. eg. if you want to set cell height according to Comment's label text.enter image description here

通过 xib 或故事板创建您的单元格。给它的插座的内容。现在在 CellForRowAtIndexPath 中调用它。例如。如果要根据注释的标签文本设置单元格高度。在此处输入图片说明

so set you commentsLbl.numberOfLine=0;

所以设置你commentsLbl.numberOfLine=0;

then in ViewDidLoad

然后在 ViewDidLoad

 self.table.estimatedRowHeight = 44.0 ;
self.table.rowHeight = UITableViewAutomaticDimension;

and now

现在

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

voila ............ you did it....

瞧…………你做到了……

回答by Christian Aberger

You can Ctrl+Drag the height constraint of your UITableView into your view controller source code and give it a name. Then in updateViewConstraints of your ViewController you can set the constant of that constraint to tableView.contentSize.height

你可以 Ctrl+Drag 你的 UITableView 的高度约束到你的视图控制器源代码中并给它一个名字。然后在 ViewController 的 updateViewConstraints 中,您可以将该约束的常量设置为 tableView.contentSize.height

....

....

@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint?

....

....

override func updateViewConstraints() {
    super.updateViewConstraints()
    tableViewHeightConstraint?.constant = tableView.contentSize.height
}

回答by Golden Thumb

It can be done programmatically. The concept (and code itself) is actually very simple:

它可以以编程方式完成。这个概念(和代码本身)实际上非常简单:

In the updateConstraints method of myTableView's superview, add a constraint so that myTableView's height is equal to myTableView.contentSize.height.

在myTableView 的superview 的updateConstraints 方法中,添加一个约束,使myTableView 的高度等于myTableView.contentSize.height。

Tested on Xcode 6 targeting iOS 7.

在针对 iOS 7 的 Xcode 6 上进行测试。

回答by Ely Dantas

Or you can satisfy height constraint from storyboard and check: [x]Remove at build time

或者您可以满足故事板的高度约束并检查: [x]Remove at build time

enter image description here

在此处输入图片说明

Then, in your controller, update tableview's height which contains all your cells stuff. (*I use PureLayout as framework over AutoLayout)

然后,在您的控制器中,更新包含所有单元格内容的 tableview 的高度。(*我使用 PureLayout 作为 AutoLayout 的框架)

enter image description here

在此处输入图片说明