2017-02-11 73 views
0

我使用带有自动布局和表格单元格内动力高度表格单元格自动布局的问题,也有1的动态高度的UILabel和1倍的动态的UIView,其中包括多个UIImageViews的。有动力高度的UILabel和UIView的(容器)的表格单元格

这里的布局: Storyboard screenshot

- (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. 

请帮我,我错过了什么。先谢谢你。

+0

您使用的故事板或XIB? –

+0

http://stackoverflow.com/a/40035858/6656894参考这个答案@Daniel –

+0

我正在使用故事板,@HimanshuMoradiya – Daniel

回答

0

你所做的工作几乎是正确的保持这种变化之下解决这个

更改视图层次内UITableviewCell &创建一个这样下面 。

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

myView约束:

  1. 顶部,底部,龙头,traling到上海华。

    edit - remove this below line

    1. height with relationship greater than equal to (≥)

注:是确保在小区的每个组件必须具有顶部和底部固定高度与关系=和标签,该标签是动态的有关系

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; 
} 

edit

视图层次

enter image description here

故事板

enter image description here

输出:

enter image description here

+0

我将动态高度标签(顶部,前导,尾部,> = 30高度约束)移动到容器视图(在您的示例中为myView)。容器视图具有> = 30的高度限制以及超级视图的顶部,前导和尾随限制。 动态标签下面添加了UIImageViews,它具有顶部(上部视图),前部和尾部(超级视图)和静态高度约束。最后的UIImageView对superview有底部约束。 但问题是一样的。 – Daniel

+0

@Daniel检查我的编辑。 –

+0

请在调试视图层次结构中向我发送您的视图层次结构截图,以便在树中展开动态标签,容器视图和图像视图约束。或者你能寄给我你有的同一个来源吗? – Daniel

相关问题