Ios 更改后,我的帧高度未更新

Ios 更改后,我的帧高度未更新,ios,swift,uitableview,constraints,Ios,Swift,Uitableview,Constraints,我在UIView中有一个UITableView,我在其中执行函数insertRowsAtIndexPaths。显示更多内容,但UITableView高度保持不变。我已经阅读了数百篇SO帖子好几天了,似乎不明白为什么这篇文章没有更新 var tableView = UITableView() tableView.frame = CGRectMake(0, 0, self.view.frame.width, view.frame.height) tableView.estima

我在UIView中有一个UITableView,我在其中执行函数
insertRowsAtIndexPaths
。显示更多内容,但UITableView高度保持不变。我已经阅读了数百篇SO帖子好几天了,似乎不明白为什么这篇文章没有更新

var tableView       = UITableView()
tableView.frame      = CGRectMake(0, 0, self.view.frame.width, view.frame.height)
tableView.estimatedRowHeight = 40
tableView.scrollEnabled = true
tableView.userInteractionEnabled  = true
tableView.rowHeight = UITableViewAutomaticDimension
tableView.delegate   = self
tableView.dataSource = self
然后,如果我单击一个单元格

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)

    self.tableView.beginUpdates()
    self.updateCells(parent, index: indexPath.row)
    self.tableView.endUpdates()

    var frame : CGRect = self.tableView.frame
    print(frame.size) // Returns -> (375.0, 667.0)
    frame.size = self.tableView.contentSize
    print(frame.size) // Return -> (375.0, 1151.0)
    self.tableView.frame = frame

}
但UITableView的高度仍为667.0,如图所示:

。。尽管您可以清楚地看到contentSize现在已经超越了界限

我可能会错过什么


更新

--下面是我如何绘制
cellforrowatinexpath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell : UITableViewCell!

    let (parent, isParentCell, actualPosition) = self.findParent(indexPath.row)

    if !isParentCell {
        cell = tableView.dequeueReusableCellWithIdentifier(childCellIdentifier, forIndexPath: indexPath)
        cell.textLabel!.text        = self.dataSource[parent].childs[indexPath.row - actualPosition - 1]
        cell.textLabel!.textColor   = UIColor(red: 35/255.0, green: 31/255.0, blue: 32/255.0, alpha: 1.0)
        cell.textLabel!.font        = UIFont(name: "STHeitiTC-Light", size: 16)
        cell.textLabel!.layer.borderColor = UIColor.purpleColor().CGColor
        cell.textLabel!.layer.borderWidth = 1.0
    }
    else {
        cell = tableView.dequeueReusableCellWithIdentifier(parentCellIdentifier, forIndexPath: indexPath)
        cell.textLabel!.text        = self.dataSource[parent].title
        cell.textLabel!.textColor   = UIColor(red: 66/255.0, green: 116/255.0, blue: 185/255.0, alpha: 1.0)
        cell.textLabel!.font        = UIFont(name: "STHeitiTC-Light", size: 20)
    }
    cell.textLabel!.frame = CGRectMake(0,0,self.view.frame.width, CGFloat.max)
    cell.selectionStyle                                       = .None
    cell.textLabel!.translatesAutoresizingMaskIntoConstraints = false
    cell.textLabel!.numberOfLines                             = 0
    cell.textLabel!.sizeToFit()
    cell.textLabel!


    return cell
}
这个问题的视觉效果


表格视图的
框架是它在superview中的实际大小。tableview的内容大小是其内容的大小。由于tableview是可滚动的,所以这两个值不相关。
frame
size是用来查看tableview内容的窗口的大小。内容可以在该窗口之外,然后您无法看到它,但您可以滚动查看所需内容。

表格视图的
框架是其superview中的实际大小。tableview的内容大小是其内容的大小。由于tableview是可滚动的,所以这两个值不相关。
frame
size是用来查看tableview内容的窗口的大小。内容可以在该窗口之外,然后您无法看到它,但您可以滚动查看所需内容。

当您向tableview添加新行时,内容大小将增加,tableview的框架将与内容大小增加后tableview滚动的框架相同

从程序上来说,这就像

tableView.userInteractionEnabled  = true
tableView.scrollEnabled = true
否则,如果应用了自动布局,则可以检查约束-从顶部、底部、左侧、右侧看,它应该像marigin一样,或者也可以指定高度/宽度,而不是底部/右侧边距


更新::自动布局 看起来自动布局是
UITableViewAutomaticDimension
的问题,根据apple doc,中有一个教程和一个答案,因此此自动维度仅在启用自动布局时有效,因此可能无法计算正确的高度

tableView.EstimateDrowehight=85.0
tableView.rowHeight= UITableViewAutomaticDimension

一旦设置了这两个属性,系统将使用“自动布局”计算行的实际高度

对于自动布局的自调整大小演示,您可以查看以下链接:

来自Appcoda

但是,如果没有自动布局,自动调整单元格大小将无法正常工作 依赖约束来确定正确的行高度

可以通过界面生成器和编程方式进行自动布局: appcoda演示中显示了编程自动布局的基本界面构建


向tableview添加新行时,内容大小将增加,tableview的框架将与内容大小增加后tableview滚动的框架相同

从程序上来说,这就像

tableView.userInteractionEnabled  = true
tableView.scrollEnabled = true
否则,如果应用了自动布局,则可以检查约束-从顶部、底部、左侧、右侧看,它应该像marigin一样,或者也可以指定高度/宽度,而不是底部/右侧边距


更新::自动布局 看起来自动布局是
UITableViewAutomaticDimension
的问题,根据apple doc,中有一个教程和一个答案,因此此自动维度仅在启用自动布局时有效,因此可能无法计算正确的高度

tableView.EstimateDrowehight=85.0
tableView.rowHeight= UITableViewAutomaticDimension

一旦设置了这两个属性,系统将使用“自动布局”计算行的实际高度

对于自动布局的自调整大小演示,您可以查看以下链接:

来自Appcoda

但是,如果没有自动布局,自动调整单元格大小将无法正常工作 依赖约束来确定正确的行高度

可以通过界面生成器和编程方式进行自动布局: appcoda演示中显示了编程自动布局的基本界面构建


当您向tableview添加新行时,contentSize将增加,tableview的框架将与tableview在内容大小之后滚动的框架相同increases@Pyro啊,有趣。听起来好像有什么东西阻止了我的tableView的可滚动性..在我的代码中还有什么东西可以让你想象,可以阻止我向下滚动以查看新内容吗?当你向tableView添加新行时,contentSize会增加,tableview的框架将与tableview在内容大小后滚动的框架相同increases@Pyro啊,有趣。听起来好像有什么东西阻止了我的tableView的可滚动性..我能在我的代码中向您展示什么,您能想象到的,可以阻止我向下滚动以查看新内容吗?很有趣。所以,我想,我注意到我的contentView确实在扩展,但它不允许我滚动它的整个新范围。你知道为什么会这样吗?或者我可以调查什么(特定的约束或值等)来找到更多关于什么可能阻止这种移动的信息?这不应该只发生在你明确指定类似的东西时,我认为你没有。你能解释一下到底发生了什么吗?插入新内容后,是否无法看到整个内容?你以前见过它吗?你的内容比最初的帧高(你可以滚动)多吗?我在问题f的底部添加了一个问题的gif