【问题标题】:Why is my UITableViewCell row forced to height 44?为什么我的 UITableViewCell 行被强制为高度 44?
【发布时间】:2017-11-07 05:41:47
【问题描述】:

当我的 UITableViewCell 行显示时,它们的高度为 44。他们绝对不应该这样做。正如您在下面有关不良约束的警告中看到的那样,我已经限制了路径的整个从上到下。

忽略它不喜欢我的 $ballIsSquare$ 高度/宽度约束的事实,我仍然应该有一个高度超过 44 的单元格。它显示几乎是圆形的球,这意味着高度几乎是 55 高(因为它显示 $ballWidth$ 为 55。但是,它根本没有显示日期标签,日期标签被限制在球上方。知道单元格是什么被逼这么小?

视图控制器为此指定估计的行高 100,并且 rowHeight 设置为 UITableViewAutomaticDimension。

我认为“UISV-alignment”和 UISV-canvas-connection 是来自正在使用的 UIStackView 的约束(例如,$ballsBottom$ 指的是球 UIStackView)

UIView-Encapsulated-Layout-Height 在我看来是真正的问题。

    "<NSLayoutConstraint:0x60400008d070 '$ballIsSquare$' DidIWin.BallView:0x7f8525a3fec0.height == DidIWin.BallView:0x7f8525a3fec0.width   (active)>",
    "<NSLayoutConstraint:0x604000091ad0 '$ballsBottom$' DidIWin.Balls:0x7f8525a3e890.bottom == UILayoutGuide:0x6040001a6c80'UIViewSafeAreaLayoutGuide'.bottom - 10   (active)>",
    "<NSLayoutConstraint:0x604000086c70 '$ballWidth$' DidIWin.BallView:0x7f8525a3fec0.width == 55   (active)>",
    "<NSLayoutConstraint:0x6040000928e0 '$largeDateBottom$' UILabel:0x7f8525a44510'10/4/17'.bottom == DidIWin.Balls:0x7f8525a3e890.top + 10   (active)>",
    "<NSLayoutConstraint:0x604000092840 '$largeDateTop$' UILabel:0x7f8525a44510'10/4/17'.top == UILayoutGuide:0x6040001a6c80'UIViewSafeAreaLayoutGuide'.top + 10   (active)>",
    "<NSLayoutConstraint:0x604000092020 'UISV-alignment' DidIWin.BallView:0x7f8525a3ead0.bottom == DidIWin.BallView:0x7f8525a3fec0.bottom   (active)>",
    "<NSLayoutConstraint:0x604000091f30 'UISV-alignment' DidIWin.BallView:0x7f8525a3ead0.top == DidIWin.BallView:0x7f8525a3fec0.top   (active)>",
    "<NSLayoutConstraint:0x60400008c670 'UISV-canvas-connection' DidIWin.Balls:0x7f8525a3e890.top == DidIWin.BallView:0x7f8525a3ead0.top   (active)>",
    "<NSLayoutConstraint:0x604000091c60 'UISV-canvas-connection' V:[DidIWin.BallView:0x7f8525a3ead0]-(0)-|   (active, names: '|':DidIWin.Balls:0x7f8525a3e890 )>",
    "<NSLayoutConstraint:0x604000091da0 'UIView-Encapsulated-Layout-Height' DidIWin.FailureDetailCell:0x7f8526047200'failure'.height == 44   (active)>",
    "<NSLayoutConstraint:0x604000091940 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6040001a6c80'UIViewSafeAreaLayoutGuide']-(0)-|   (active, names: '|':DidIWin.FailureDetailCell:0x7f8526047200'failure' )>",
    "<NSLayoutConstraint:0x6040000918a0 'UIViewSafeAreaLayoutGuide-top' V:|-(0)-[UILayoutGuide:0x6040001a6c80'UIViewSafeAreaLayoutGuide']   (active, names: '|':DidIWin.FailureDetailCell:0x7f8526047200'failure' )>"

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400008d070 '$ballIsSquare$' DidIWin.BallView:0x7f8525a3fec0.height == DidIWin.BallView:0x7f8525a3fec0.width   (active)>

【问题讨论】:

    标签: xcode interface-builder xcode9


    【解决方案1】:
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        if(indexPath.section == 0){
            return 50
        }else{
            return 120
        }
    }
    

    在你的视图控制器中添加这个委托方法

    或者你可以使用

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
           return UITableViewAutomaticDimension
    }
    

    自动标注

    【讨论】:

    • 不,这不是我想要的。它不是一个静态大小的单元格。它是动态大小的,基于设置的约束。
    • 你展示的委托方法没用。高度方法并不是真正需要作为委托的。它已经在桌子上设置好了。另请注意,我说我已经在问题的第三段中设置了该设置。问题是问为什么我要根据显示的约束将高度强制为 44。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多