2017-06-22 44 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

相关问题