Swift:使用prepareForSegue将UIView容器的高度设置为嵌入式UITableView的高度

索斯皮尔

我有一个UITableViewController嵌入在UIView容器中,这是在一个底部UIScrollViewUIViewController这是情节提要中当前配置的屏幕截图。

屏幕截图

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

  1. Set the identifier of the embed segue between the UIView and the UITableViewController to "embedSegue"
  2. At the top of the the embedded UITableViewController class, create a variable to hold the height of the table:

    var tableHeight: CGFloat!
    
  3. 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)
    }
    
  4. In the UIViewController class that contains the UIView container, create a variable to hold the value passed from the UITableViewController:

    var viewHeight: CGFloat!
    
  5. 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)
            }
        }
    }
    
  6. 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:

  • .sizeThatFits()
  • .needsUpdateConstraints()
  • .layoutIfNeeded()

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.

K. Thorspear

而不是使用的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何自动将嵌入式SVG缩放为父容器的宽度和高度?

调整容器视图的高度以匹配嵌入式UITableViewController

CSS:获取嵌入式图像以使用容器的宽度和高度的100%

将容器类的高度设置为图像的高度

Swing:如何将组件高度设置为容器的高度?

使用自动布局将UITableView的高度设置为其内容的高度

将父容器设置为悬停的子容器的高度

将动态高度设置为UIScrollView Swift

是否可以仅使用css将div的高度设置为容器减去另一个div的高度?

将容器高度设置为等于Flutter中的宽度

将传单地图设置为高度:容器的100%

无法将悬停叠加层的高度设置为容器的100%

将容器设置为固定高度并可以滚动

将容器div设置为与iframe相同的高度

将最小高度设置为100%作为最小高度为100%的容器的子代

将viewpager的高度设置为内容的高度

将UITableViewCell的高度设置为UIImage的高度?

使用“嵌入式”密钥将JSON解组为结构

基于元素高度的方形嵌入式按钮

获取嵌入式Tableau仪表板的窗口高度

getBoundingClientRect,嵌入式SVG的高度值不正确

GoodData中的动态嵌入式仪表板高度

如何将SVG的高度设置为其封闭容器的高度?

将容器div的最小高度设置为其孙子div的最大高度

我如何将最小高度设置为我的容器,然后在点击按钮后包装内容高度等于文本高度

iOS:在Swift中动态设置UITableView的高度

使多个嵌入式div的高度与其父div的高度相同

当最小高度设置为容器高度时,为什么不继承容器高度?

我们如何将父容器的高度设置为子容器