2017-02-11 78 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

相關問題