Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何删除UITableView';s NSAutoResizingMaskLayoutConstraint?_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

Ios 如何删除UITableView';s NSAutoResizingMaskLayoutConstraint?

Ios 如何删除UITableView';s NSAutoResizingMaskLayoutConstraint?,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,通过在单元格的contentView中添加一些子视图,我设计了一个自定义UITableViewCell,并且在contentView和子视图之间添加了一些自动布局约束 但当我调试应用程序时,Xcode告诉我存在约束冲突。在约束列表中,有一个NSAutoResizingMaskLayoutConstraint将单元格高度限制为43,因此Xcode会打破我的子视图高度约束并“压缩”它 我试过: 在Interface builder中,取消选中“自动重新生成子视图”复选框。不起作用 在代码中,cell

通过在单元格的contentView中添加一些子视图,我设计了一个自定义UITableViewCell,并且在contentView和子视图之间添加了一些自动布局约束

但当我调试应用程序时,Xcode告诉我存在约束冲突。在约束列表中,有一个NSAutoResizingMaskLayoutConstraint将单元格高度限制为43,因此Xcode会打破我的子视图高度约束并“压缩”它

我试过:

  • 在Interface builder中,取消选中“自动重新生成子视图”复选框。不起作用

  • 在代码中,
    cell.contentView.translatesAutoResizingMaskIntoConstraints=NO
    。这会导致应用程序崩溃,出现一个异常:“执行-layoutSubviews后仍然需要自动布局”。在这个问题上,我已经尝试了所有建议的解决方案:没有一个对我有效

  • 所以我想我只能让单元格自动调整大小,并删除代码中的自动调整大小约束。我该怎么做才能不弄坏东西

    编辑:
    或者,从另一个角度看,如何使tableViewCell高度灵活(随子视图高度和约束而变化)?在IB中,我必须设置它的高度,对吗?

    您不需要设置
    cell.contentView.translatesAutoResizezingMaskintoConstraints=NO

    为了在autolayout中的UITableViewCells中获得灵活的高度,您需要手动计算
    -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath中的高度

    是的,这很乏味,但没有其他办法。要自动计算高度,需要在UITableViewCell中满足两个条件:

  • 必须确保所有子视图都具有
    translatesAutoResizingMaskIntoConstraints=NO
  • 单元子视图的约束必须推压UITableViewCell的上边缘和下边缘
  • 然后在
    -(CGFloat)tableView:(UITableView*)tableView HeightForRowatineXpath:(NSIndexPath*)indexPath
    中,需要为特定的indexPath重新创建该单元格,并手动计算该单元格的高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath 
    *)indexPath
    {
    
     //Configure cell subviews and constraints
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
     [self configureCell:cell forIndexPath:indexPath];
    
     //Trigger a layout pass on the cell so that it will resolve all the constraints.
     [cell setNeedsLayout];
     [cell layoutIfNeeded];
    
     //Compute the correct size of the cell and get the height. This is where the magic happens.
     CGFloat height = [cartItemCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        height += 1.0f
    
     return height;
    
    }
    
    请注意,
    systemLayoutSizeFittingSize
    对于uitextview是不可靠的。在这种情况下,您必须使用另一种方法手动计算整个单元的高度。通过缓存每个索引XPath的高度,还可以进行一些性能优化


    这篇博文对该做什么做了更详细的描述,但要点基本上就是我上面提到的

    我已经为带有自动布局的动态tableview单元格高度创建了一些示例代码。您可以使用以下链接下载该项目


    我希望这将对您有所帮助。

    一定有一些布局冲突。创建一些示例项目并与我们共享。那就很容易弄明白了。如果你想知道如何使用自动布局创建动态单元格高度,那么我创建了一些示例项目,我可以在这里分享。谢谢!我想我的问题是我使用Interface builder来布局子视图。默认情况下,IB定义单元的固定高度,该高度转换为布局约束并导致冲突。所以你的建议是我删除.xib文件并编写代码来布局子视图,对吗?或者有没有办法删除interface builder中固定高度的单元格?正如您所说,用代码布局视图非常繁琐,因为有许多边距限制。实际上,您不需要删除IB来实现tableView:heightForRowAtIndexPath。只要在UITableViewDelegate(很可能是UITableViewController)的代码中实现tableView:heightForRowAtIndexPath,您仍然可以更改单元格行的高度。如果已在中设置了约束,则可以通过在单元格中添加所需的任何内容(UILabel等)来配置单元格,以将其展开并调用systemLayoutSizeFittingSize。