简体   繁体   English

使用单元格大小自动布局调整tableview框架的大小以适合动态内容

[英]Resize tableview frame to fit dynamic contents using cell size autolayout

When I resize my tableView , content is cut off because contentSize seems to be determined by tableView.estimatedRowHeight = 18 . 调整tableView大小时,内容将被切断,因为contentSize似乎由tableView.estimatedRowHeight = 18确定。

The actual contentSize of the tableView (adjusted for autolayout) is larger than sizeToFit() thinks it is. tableView的实际contentSize (已针对autolayout进行调整)大于sizeToFit()认为的大小。

How do I tell sizeToFit() the correct size of my tableView contents? 如何告诉sizeToFit()正确的tableView内容大小?

 tableView.frame = CGRectMake( 0, self.scrollView.frame.size.height + 18, self.view.frame.size.width, 350 )
 tableView.estimatedRowHeight = 18
 tableView.rowHeight = UITableViewAutomaticDimension

 self.view.addSubview(self.tableView)
 tableView.sizeToFit()

How do I tell sizeToFit() the correct size of my tableView contents 我如何告诉sizeToFit()我的tableView内容的正确大小

You don't. 你不知道 The table view, as a view, knows nothing of how many rows it has or even that you think it should be the height of its rows. 表格视图作为一个视图,不知道它有多少行,甚至不知道它应该是其行的高度。 (It can't read your mind.) You must calculate its height yourself based on the the number of rows and their heights. (它不能读你的心。)您必须计算基于行和其高度的数量及其自己的高度。

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

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