【问题标题】:Dynamic height TableView in a Scroll View滚动视图中的动态高度 TableView
【发布时间】:2016-03-30 13:42:02
【问题描述】:

我正在设计一个具有滚动视图的页面,其上方是一个表格视图(禁用滚动)。为此,我在这个问题中提到了答案 - Make UITableView not scrollable and adjust height to accommodate all cells,但没有成功。

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

-主视图

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

-内容视图

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

-内容视图内的表格视图

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

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

 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 也不服从自动布局约束。

【问题讨论】:

  • 我猜你不应该在cellForRowAtIndexPath中调用tableview.frame.size = tableview.contentSize
  • @aaisataev 没错。看看我的回答
  • 参考这些链接.. stackoverflow.com/questions/19036228/…> developer.apple.com/library/ios/documentation/UserExperience/…>。希望对您有所帮助... :)

标签: ios swift uitableview uiscrollview autolayout


【解决方案1】:

只是我需要这样做 -

  • 删除线 - tableView.frame.size = tableView.contentSize

  • 为表格视图添加高度约束。

  • 将优先级设置为高

  • 创建高度约束的出口(Ctrl+Drag)。

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

tableHeightConstraint.constant = tableview.contentSize.height

【讨论】:

  • 它对我有用,但 tableview 底部显示了一些额外的空间?
  • 我使用了你的方法,只是改变了计算 tableviewHeight 的方式,因为对我来说 tableview.contentSize.height 没有按预期工作。这是我的方式:rowHeight * numberOfRows
  • 这不起作用。 @user5479794.@Isaac Bosca 如果您设置行高,则它不是自动布局。
猜你喜欢
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 2017-12-07
  • 1970-01-01
相关资源
最近更新 更多