ios - UITableView 在禁用滚动的情况下动态增加高度?

标签 ios swift uitableview

我正在设计一个应用程序,它有一个 ScrollView ,其上有一个内容 View ,该内容 View 有一个禁用滚动的表格 View 。现在我希望 TableView 能够根据我的数组内容动态增加其大小。我已经做了一些编码,并且将我的约束屏幕截图放在其中,以便让事情变得更清楚 -

我的约束列表——

enter image description here

现在这是我的代码,用于填充表格 View 并将其大小增加到内容 View -

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


    }

这是我的主要 Storyboard截图 enter image description here

所以现在当我运行代码时,内容大小不会根据 TableView 的大小而增加。这是 4s 上的样子 -

enter image description here

那么如何根据动态支持 IB 中规定的所有约束的 TableView 增加​​的高度来调整我的内容 View ?

最佳答案

按照这些说明进行操作。
需要约束:-
MainView 到 ScrollView - leading = 0, trailing = 0, top = 0, bottom = 0;
ScrollView 到 ContentView -leading = 0,trailing = 0,top = 0,bottom = 0,contentView.width = mainView.width,contentView.height =“一些常量”
ContentView 到 TableView - leading = 8,trailing = 8,top = 518(根据您的屏幕截图),bottom = 64;

现在需要为 contentView 的高度约束创建一个 IBOutlet,我们之前已经给了它一个随机常量值;我们称它为 contentViewHeightConstraint

现在,一旦您获得数组中的记录,您需要调用 reloadData 并根据表中的记录数增加 contentView 的高度。

contentViewHeightConstraint.constant = 518 + 44*(name.count) + 64;
同时删除 cellForRowAtIndexPath: 中用于设置 tableView 大小的代码。

关于ios - UITableView 在禁用滚动的情况下动态增加高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34435627/

相关文章:

ios - 在客户端之间传递连接状态

ios - 我将如何重构以下代码以使用 NSSet.objectsPassingTest()

ios - collectionView 中的 TapGesture

ios - 如何设置tableviewcell的自动布局约束

ipad - 弹出窗口中的导航 Controller

ios - 用于短信和通话的 openUrl 替代方案

ios - 在 Macos 中运行 Ejabberd 服务器

iphone - tableview 部分标题中的选项卡式导航

ios - numberOfRowsInSection 在 iOS8 中被调用了太多次?

ios - 我如何像引用 IBOutlet 一样在代码中引用 gamescene.sks 中添加的 Sprite ?