Autolayout约束不匹配在UITableViewCell中

时间:2017-06-08 12:52:36

标签: ios objective-c uitableview autolayout tableviewcell

为动态单元格高度创建简单的应用程序。

我有自定义UITableViewCell,我有一些控件。我修复了所有控件的约束。 Tableview在约束设置后不显示任何错误。 Tableview运行时单元格高度没问题,但是当我检查控制台日志时,它给出了我的错误,如下所示。

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.
2017-06-08 17:54:22.184177+0530 Test[46461:306448] [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:0x600000286680 UILabel:0x7fe86bd1c4e0'Label'.height == 50   (active)>",
    "<NSLayoutConstraint:0x600000286810 V:|-(14)-[UILabel:0x7fe86bd1c4e0'Label']   (active, names: '|':UITableViewCellContentView:0x7fe86bd1c220 )>",
    "<NSLayoutConstraint:0x600000286860 V:[UILabel:0x7fe86bd1c4e0'Label']-(15)-|   (active, names: '|':UITableViewCellContentView:0x7fe86bd1c220 )>",
    "<NSLayoutConstraint:0x600000286cc0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe86bd1c220.height == 49   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000286680 UILabel:0x7fe86bd1c4e0'Label'.height == 50   (active)>

Bellow是我的TableView及其约束。 enter image description here

Bellow是我的TableView代码

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSString * strAction = arrActionList[indexPath.row];
    if ([strAction isEqualToString:@"More"]) {
        cell.lblActionHeight.constant = 50;
    }
    else{
        cell.lblActionHeight.constant = 20;
    }
    cell.lblAction.text = strAction;
    [cell.contentView layoutIfNeeded];
    return cell;
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

控制台日志的含义:

your_cell_height(79) = top(14) + lbl_ht(50) + bottom(15) // from storyboard

但是行的单元格

your_cell_height为79,根据它,tablecell管理如此

your_cell_height(49) = top(14) + lbl_ht(20) + bottom(15) // from CellForRow Method

所以49不等于79这就是为什么存在约束冲突。

<强>解析。

删除底部约束并尝试。

如果您有任何指定的单元格布局,那么具体,根据我的建议。

  

修改

在带有约束的视图中添加标签:(L,R,T,B)=(0,0,0,0) height = 79 关系less than equal to

给予标签与此处给出的约束相同的约束。

添加以下代表方法

cellForHeight

return UITableViewAutomaticDimension

和估计高度

return 79

答案 1 :(得分:0)

如果你不想给出身高限制,你应该给出顶部,底部,领先,尾随。

如果你需要身高限制,那么你应该给(领先,尾随,顶部,身高)或(领先,尾随,中心,身高)

确保在heightForRow方法中返回正确的高度。

如果要使用UITableViewAutomaticDimension

,则需要指定估计的行高

在您的情况下,estimatedRow height是IB中的确切行高。

相关问题