滚动视图中的动态高度 TableView

时间:2022-11-09
本文介绍了滚动视图中的动态高度 TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在设计一个具有滚动视图的页面,其上方是一个表格视图(禁用滚动).为此,我在这个问题中提到了答案 - 使 UITableView 不可滚动并调整高度以适应所有单元格,但没有成功.

I am designing a page having a scroll view and above it a table view(scroll disabled). For doing this I have referred answers in this question - Make UITableView not scrollable and adjust height to accommodate all cells ,but wasn't successful.

视图的层次结构以及提供的约束-

Hierarchy of views along with provided constraints-

-主视图

-滚动视图
固定到主视图的所有侧面(0,0,0,0),限制到边距

-Scroll view
pinned to all sides of main view(0,0,0,0), constraint to margins

-内容视图

固定到滚动视图(0,0,0,0),与主视图等宽,与主视图等高(优先级 - 250)

pinned to scroll view(0,0,0,0),equal width to main view,equal height to main view(priority - 250)

-内容视图内的表格视图

滚动禁用,从四面八方有50个点空间,高度(> =),底部间距50(关系> =).我已经把大于等于,以便动态增加高度.

scroll disabled,having 50 point spaces from all sides,Height(>=),bottom spacing 50(relation >=).I have put greater than equal so as to increase height dynamically.

现在,当我填充表格视图时,我将代码用作

Now when I populate my table view I use the code as

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath) 
        cell.textLabel?.text = name[indexPath.row]

        tableview.frame.size = tableview.contentSize

        return cell
    }

因此,当我运行我的代码时,它会增加 tableview 框架但不会拉伸内容大小,而且它会变得很奇怪,因为我的滚动视图不会滚动到 table view 的末尾,我的 table view 也不服从自动布局约束.

So when I run my code, it increases the tableview frame but doesn't stretch the content size and it just becomes weird as my scroll view doesn't scroll to the end of the table view neither my table view obeys the auto layout constraints.

推荐答案

只是我需要这样做 -

Just I needed to do this -

  • 去掉这一行 - tableView.frame.size = tableView.contentSize

为表格视图添加高度约束.

Add a height constraint for table view.

将优先级设置为高

创建一个高度约束的出口(Ctrl+Drag).

Create an outlet of the height constraint(Ctrl+Drag).

无论您需要在何处重新加载表格数据,请将高度约束设置为表格视图的内容高度.

Wherever you need to reload data of your table, set the height constraint to tableview's content height.

tableHeightConstraint.constant = tableview.contentSize.height

这篇关于滚动视图中的动态高度 TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:以编程方式将 UIScrollView 滚动到 Swift 中的子 UIView(子视图)的顶部 下一篇:如何在 UITableView 下方添加活动指示器?

相关文章

最新文章