通过其内容动态设置 UIScrollView 和 UITableView 高度 [英] Dynamically set UIScrollView and UITableView height by its content

查看:59
本文介绍了通过其内容动态设置 UIScrollView 和 UITableView 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 iOS 开发很陌生,我已经坚持了几天

Im quite new to iOS development and i've been stuck with this for a couple of days

我在 UIScrollView 中有一个 UITableViewUIImageViewUIImageView 位于表格的顶部,高度是静​​态的,所以我没有任何问题,问题是 UITableView,我需要根据内容动态获取高度并将该高度设置为表格的高度约束以及滚动视图的内容大小.

i have a UITableView and UIImageView inside a UIScrollView, the UIImageView is on top of the table and the height is static so i don't have any issues with that, the problem is the UITableView, i need to get the height dynamically based on the content and set that height to a height constraint of the table and the contentsize of the scrollview.

单元格是自动调整大小的,因此单元格的高度因内容而异.到目前为止,我能做的最好的事情是使用 UITableViewcontentSize.height 属性获取高度,但显然这会返回基于 estimatedRowHeight 的高度我设置,而不是每行的实际高度.

The cells are self sizing, so the height of the cell varies depending on the content. The best i could do so far is getting the height by using contentSize.height property of UITableView but apparently this returns a height based on the estimatedRowHeight i set, not te actual height of each row.

代码:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.dataSource = self
    tableView.delegate = self
    tableView.estimatedRowHeight = rowHeight
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.layoutIfNeeded()
    tableHeight.constant = tableView.contentSize.height
    scrollView.contentSize.height = imageView.bounds.height + tableView.contentSize.height
}

我尝试过的:

  • 使用UITableViewcontentSize.height 属性获取高度,如代码所示,之前已经解释过.
  • willDisplayCell 上单独获取每一行的高度,并将其添加到表格的高度约束和滚动视图的 contentSize 中.但是看起来这比实际增加了更多的行,在表格的末尾给了我一堆空单元格.willDisplayCell 里面的代码:

  • Getting the height by using contentSize.height property of UITableView, as shown in the code and explained before.
  • Getting the height of each row individually on willDisplayCell and add it to the height constraint of the table and the contentSize of the scrollview. But looks like this adds more rows than actually are, giving me a bunch of empty cells at the end of the table. the code inside willDisplayCell:

let cellHeight = cell.bounds.height
tableHeight.constant += cellHeight
scrollView.contentSize.height += cellHeight

  • 和以前一样,除了我在 cellForRowAtIndexPath 中尝试了相同的结果.

  • The same as before, except i tried inside cellForRowAtIndexPath with the same result.

    推荐答案

    Apple 文档 不鼓励在 UIScrollView 中嵌入 UITableView:

    The Apple documentation discourages embedding UITableView within a UIScrollView:

    您不应在 UIScrollView 对象中嵌入 UIWebView 或 UITableView 对象.如果这样做,可能会导致意外行为,因为两个对象的触摸事件可能会混淆并错误处理.

    You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

    要解决您的问题,您可以将 UIImageView 放在标题视图中,或者更改第一行的单元格以显示图像.然后,您将使用 UITableViewDelegate 调整单元格高度,特别是 tableView:heightForRowAtIndexPath:.

    To solve your problem, you could put the UIImageView in a header view, or change the cell of your first row to display an image. You would then adjust the cell heights using your UITableViewDelegate, specifically tableView:heightForRowAtIndexPath:.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // adjust the return based on the indexPath
        return 30;
    }
    

    这篇关于通过其内容动态设置 UIScrollView 和 UITableView 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆