2017-06-22 115 views
3

我試圖實現一個UITableViewCell,它會自動調整其高度以適合可用內容。我現在有下面的佈局,但是每當我運行程序時,調試器會拋出各種「無法同時滿足約束」錯誤。我設置約束的方式有什麼問題嗎?自動調整大小UITableViewCell:無法同時滿足約束

[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:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60 (active)>", 
    "<NSLayoutConstraint:0x60800009a8b0 UIImageView:0x7fd389002f50.top == UITableViewCellContentView:0x7fd389009b20.topMargin + 4 (active)>", 
    "<NSLayoutConstraint:0x608000097ca0 UITableViewCellContentView:0x7fd389009b20.bottomMargin >= UIImageView:0x7fd389002f50.bottom + 4 (active)>", 
    "<NSLayoutConstraint:0x600000097d40 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fd389009b20.height == 80 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800009c2f0 UIImageView:0x7fd389002f50.height == 60 (active)> 

enter image description here

enter image description here

enter image description here

爲了完整起見,這裏是簡單的代碼,我使用的測試。

「ListViewController.m」

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    tableView.rowHeight = UITableViewAutomaticDimension; 
    tableView.estimatedRowHeight = 40; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 40; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ListTableViewCell *cell = (ListTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath]; 

    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 

- (void)configureCell:(ListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    cell.hasProfile = (arc4random_uniform(10) < 3); 
    cell.hasSecondField = (arc4random_uniform(10) < 3); 
} 

ListViewCell.h

@interface ListTableViewCell : UITableViewCell { 
    IBOutlet NSLayoutConstraint *pictureWidthConstraint; 
    IBOutlet NSLayoutConstraint *pictureHeightConstraint; 
    IBOutlet NSLayoutConstraint *pictureBottomTrailingConstraint; 
    IBOutlet NSLayoutConstraint *subheaderHeightConstant; 

    IBOutlet UILabel *subheaderLabel; 
} 

@property (nonatomic, assign) BOOL hasProfile; 
@property (nonatomic, assign) BOOL hasSecondField; 

@end 

ListViewCell.m

- (void)setHasProfile:(BOOL)hasProfile { 
    _hasProfile = hasProfile; 

    if (!hasProfile) { 
     pictureHeightConstraint.constant = 0; 
     pictureWidthConstraint.constant = 0; 
    } 
    else { 
     pictureHeightConstraint.constant = 60; 
     pictureWidthConstraint.constant = 60; 
    } 
} 

- (void)setHasSecondField:(BOOL)hasSecondField { 
    _hasSecondField = hasSecondField; 

    if (!hasSecondField) { 
     subheaderLabel.text = @""; 
     subheaderHeightConstant.constant = 0; 
    } 
    else { 
     subheaderLabel.text = @"Second Label"; 
     subheaderHeightConstant.constant = 21; 
    } 
} 
+0

由於錯誤地明確指出,問題與Imageview高度有關。您已經給出了imageview = 60的高度以及底部和頂部限制。這就是它給你提供不滿意約束的警告的原因。刪除imageview的底部約束,看看它是否給出錯誤。 –

+0

在這裏看到我的答案關於autoLayout的'TableCell' ,,,,, https://stackoverflow.com/a/43656451/4466607 @ PF1 – Dhiru

+0

嘗試在uitableviewcell的檢查器中爲行高設置checkBox「Custom」。如果存在,則刪除uitableviewcell的約束約束80。另外我會刪除pictureHeightConstraint.constant = 0和subheaderHeightConstant.constant = 0 – Malder

回答

1

該錯誤表示還有一些其他約束與您的圖像視圖的高度相沖突60,並且修改此約束以便滿足所有約束。

您可以調整週圍以查看哪個約束與該高度衝突,或者您可以自行分解。爲此,請單擊圖像高度上的編輯按鈕,然後將優先級更改爲999 enter image description here

相關問題