当前位置: 首页 > 知识库问答 >
问题:

具有动态高度UILabel和UIView(容器)的表格单元格内的自动布局问题

欧阳德运
2023-03-14

我使用自动布局的动态高度表格单元格,在表格单元格内部,有1个动态高度UILabel和1个动态UIView,其中包括多个UIImageView。

这是布局:情节提要屏幕截图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     tableView.estimatedRowHeight = 500;
    return UITableViewAutomaticDimension;
}

TableCell:

for(i=0; i<subviews.count; i++)

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_image.png"]];

    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;

    [_viewAttaches addSubview:imgView];
    imgView.translatesAutoresizingMaskIntoConstraints = NO;

    if(i==0) {
        NSLayoutConstraint *top = [NSLayoutConstraint
                                       constraintWithItem:imgView
                                       attribute:NSLayoutAttributeTop
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:_viewAttaches
                                       attribute:NSLayoutAttributeTop
                                       multiplier:1.0f
                                       constant:0.f];
        [_viewAttaches addConstraint:top];
     } else {
             UIImageView *prevView = subviews[i-1];
             NSLayoutConstraint *top = [NSLayoutConstraint
                                               constraintWithItem:imgView
                                               attribute:NSLayoutAttributeTop
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:prevView
                                               attribute:NSLayoutAttributeBottom
                                               multiplier:1.0f
                                               constant:10.f];
              [_viewAttaches addConstraint:top];
           }

           NSLayoutConstraint *leading = [NSLayoutConstraint
                                               constraintWithItem:imgView
                                               attribute:NSLayoutAttributeLeading
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:_viewAttaches
                                               attribute:NSLayoutAttributeLeading
                                               multiplier:1.0f
                                               constant:0.f];

            [_viewAttaches addConstraint:leading];

            NSLayoutConstraint *trailing = [NSLayoutConstraint
                                                constraintWithItem:imgView
                                                attribute:NSLayoutAttributeTrailing
                                                relatedBy:NSLayoutRelationEqual
                                                toItem:_viewAttaches
                                                attribute:NSLayoutAttributeTrailing
                                                multiplier:1.0f
                                                constant:0.f];

            [_viewAttaches addConstraint:trailing];


            NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:imgView
                                                                              attribute:NSLayoutAttributeHeight
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:nil
                                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                                             multiplier:1
                                                                               constant:160];
            [_viewAttaches addConstraint:height];


                if(i==nAttaches-1) {
                    NSLayoutConstraint *bottom = [NSLayoutConstraint
                                                  constraintWithItem:imgView
                                                  attribute:NSLayoutAttributeBottom
                                                  relatedBy:NSLayoutRelationEqual
                                                  toItem:_viewAttaches
                                                  attribute:NSLayoutAttributeBottom
                                                  multiplier:1.0f
                                                  constant:0.f];
                    [_viewAttaches addConstraint:bottom];
                }

                i++;
}

但我犯了这个错误:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 

<NSLayoutConstraint:0x174288390 UILabel:0x101157580'Daniel'.height == 21   (active)>,
"<NSLayoutConstraint:0x174288cf0 UILabel:0x101158cb0'0 comments'.height == 21   (active)>",
"<NSLayoutConstraint:0x174288e80 UILabel:0x101157580'Daniel'.top == UITableViewCellContentView:0x101157130.topMargin + 2   (active)>",
"<NSLayoutConstraint:0x174288fc0 V:[UILabel:0x101157580'Daniel']-(-1)-[UILabel:0x1011469b0'I am currently in a happy...']   (active)>",
"<NSLayoutConstraint:0x1742890b0 V:[UILabel:0x1011469b0'I am currently in a happy...']-(0)-[UIView:0x101158b10]   (active)>",
"<NSLayoutConstraint:0x174289240 V:[UIView:0x101158b10]-(-1)-[UILabel:0x101158cb0'0 comments']   (active)>",
"<NSLayoutConstraint:0x1742892e0 UITableViewCellContentView:0x101157130.bottomMargin == UILabel:0x101158cb0'0 comments'.bottom + 1   (active)>",
"<NSLayoutConstraint:0x170284ec0 UIView:0x101158b10.height == 330   (active)>",
"<NSLayoutConstraint:0x17428a640 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x101157130.height == 89   (active)>"


Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170284ec0 UIView:0x101158b10.height == 330   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

请帮助我,我错过了什么。提前谢谢你。

共有1个答案

百里文景
2023-03-14

你所做的工作几乎是正确的保持这一点以下的变化来解决这个问题

1. 在 UI 表视图单元格中更改视图层次结构

-> Timeline TableView
  '-> postecell
     '-> content view
        '-> myView // I added this `UIview` & in this view add all you cell components
          '-> Img User
          '-> lbl Name
          :    :
          :    :

< code>myView约束:

> < li >顶部、底部、领导、培训到监督。 < blockquote > < p >编辑-删除该行下面的内容

注意:确保单元格中的每个组件都必须有顶底和固定高度,关系=和动态标签具有关系

2.你对身高的定义是这样的

- (CGFloat)tableView:(UITableView *)tableView    estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return 250; // it is the height of cell which is in the storyboard after creation of custom cell.
}   

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension; 
}

编辑

查看层次结构

故事板

输出:

 类似资料:
  • 我有一个带有一堆单元格的表视图(自定义单元格,它只有它的内容视图)。 在 中,我正在向自定义单元格的content视图添加一个预定义的UIView(它有几个子视图)。我之前为UIView及其子视图设置了所有约束。 最后但并非最不重要的是,我为我的自定义单元格的内容视图(superview)和UIView设置了垂直和水平约束,UIView是在(subview)之前添加的。 约束字符串如下所示: 不幸

  • 对于我的表视图单元格的动态高度,我从此链接中引用。在 UITableView 中使用自动布局进行动态单元格布局 这是我的表视图数据源和委托方法的代码 我有 2 个问题: > 我的问题是我在线路附近收到错误 在和。 为什么我必须在单元格中同时写入标签文本“和? 另外,我是否缺少实现单元动态高度所需的任何内容?

  • 我有一个UIScrollView,嵌套在一个内容视图中,它有两个嵌套的子视图,一个具有已知高度的常规UIView,以及一个具有动态高度的容器视图,具体取决于内容。就像这样: 视图如下所示: 我的约束设置如下: 滚动视图被限制在其超级视图(即视图)的尾缘、前缘、顶端和下边缘 内容视图被约束到其超级视图(即滚动视图)的尾部、前导、顶部和底部边缘。它还具有与主视图(即视图)相等的宽度约束,因此滚动视图的

  • 我使用界面生成器创建了一个自定义的 TableView 单元格。这是它的样子: 对于描述标签,我需要它来自动换行,所以我这样设置它: 在我的设置页面视图控制器中,我重写了以下表视图方法: 生成的页面如下所示: 如您所见,第一个tableview单元格的描述标签没有自动换行。它就这么断了。我如何使它包装? 此外,我想动态调整表格视图单元格的高度。我试图改变高度,使其看起来更怪异: 如何为1行描述标签

  • 我有一个具有动态高度的表视图与其他具有动态高度的UI组件一起在表视图中 数据,模型在这里.. 单元格子视图。。 这是内部tableView及其约束! 构造器

  • 场景- > TableView高度是使用纵横比相对于superview的高度动态设置的。 TableViewCell的高度是根据表格视图的高度计算的: 问题 - 最初在委托方法中没有正确计算表格视图高度。但在滚动单元格后正确计算。 尝试的解决方案: 在视图中重新加载。 调用 在单元格中进行. 有什么方法可以正确计算出这个表格的高度吗?