我有一个UITableViewController
嵌入在UIView
容器中,这是在一个底部UIScrollView
中UIViewController
。这是情节提要中当前配置的屏幕截图。
在UITableView
将包含选中的帖子回应。因为的高度和响应数UITableView
将是可变的,所以我希望UIView
容器的高度根据表的高度而变化。当前(不希望的)行为是UIView
容器保持恒定的高度,并在UITableView
高度大于容器的高度时滚动UIView
。
The most viable method seems to be having the UITableViewController
pass the height of the table to the UIViewController
using prepareForSegue
, and then set the UIView
's height to that passed value. However, I can't seem to set a value with the height of the table until after prepareForSegue is run.
What Isn't Working
UIView
and the UITableViewController
to "embedSegue"At the top of the the embedded UITableViewController
class, create a variable to hold the height of the table:
var tableHeight: CGFloat!
Still in the embedded UITableViewController
, override viewDidLoad
like so, printing passedHeight to the console for checking:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 68.0
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableHeight = self.tableView.contentSize.height
print(tableHeight)
}
In the UIViewController
class that contains the UIView
container, create a variable to hold the value passed from the UITableViewController
:
var viewHeight: CGFloat!
Still in the UIViewController
, override prepareForSegue
like so, setting viewHeight to the passed tableHeight value, and printing viewHeight to the console for checking:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "embedSegue" {
let responseTableView = segue.destinationViewController as! ResponseViewController
self.viewHeight = responseTableView.tableHeight
if viewHeight == nil {
print("Nil")
} else {
print(viewHeight)
}
}
}
When I run the app, viewHeight prints as Nil first, then tableHeight prints, which means prepareForSegue
has run prior to tableHeight being set.
I have also tried other methods besides prepareForSegue
, including calling various functions on the UIView
container in viewDidLayoutSubviews() like:
However, the UIView
container remains the same height as set in the storyboard.
As an epilogue, this is my first StackOverflow post, and I'm excited to be joining the community. Please do let me know if I can clarify my question. Thank you for taking the time.
而不是使用的UIViewController
具有UIScrollView
包含三个UIViews
,我创建了一个UITableView
有三个定制单元。每个单元都有自己的自定义UITableViewCell
类以提高灵活性。
这意味着我失去了使用约束条件来动态更改每个部分的高度的能力。我希望帖子标题和帖子文本以全屏图像为中心。为此,我添加了以下逻辑:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
//Fullscreen image cell - the "20" is for the status bar
return tableView.frame.size.height - self.navigationController!.navigationBar.frame.size.height - 20
} else {
//Just use the current rowHeight
return self.tableView.rowHeight
}
}
如何将UITableView嵌入UIScrollview
中一个类似问题的简短答案引发了这个想法
带有多个带有Swift的自定义单元格的UITableview
关于如何创建UITableView
带有3个自定义项的详细说明UITableViewCells
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句