Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 自动调整UITableViewCell的大小:无法同时满足约束_Ios_Objective C_Xcode_Uitableview_Uikit - Fatal编程技术网

Ios 自动调整UITableViewCell的大小:无法同时满足约束

Ios 自动调整UITableViewCell的大小:无法同时满足约束,ios,objective-c,xcode,uitableview,uikit,Ios,Objective C,Xcode,Uitableview,Uikit,我正在尝试实现一个UITableViewCell,它可以自动调整其高度以适应可用内容。我现在有以下布局,但每当我运行程序时,调试器都会抛出各种“无法同时满足约束”错误。我设置约束的方式是否有问题 [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't wa

我正在尝试实现一个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)>
ListViewCell.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);
}
- (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;
    }
}

该错误表示存在与imageview的高度60冲突的其他约束,并且该约束已修改,以便满足所有约束

您可以左右调整以查看哪个约束与该高度冲突,也可以自己打破它。要执行此操作,请单击“图像高度”上的“编辑”按钮,然后将优先级更改为999

由于明确指出错误,问题在于Imageview高度。您已经给出了imageview的高度=60以及底部和顶部约束。这就是为什么它会给你不满意的警告。删除imageview的底部约束并查看是否出现错误。请参阅我在此处关于自动布局
表格单元格的回答,,,,,@PF1try以在uitableviewcell的检查器中设置行高的“自定义”复选框。并删除uitableviewcell的约束80(如果存在)。此外,我还将删除picturehightconstraint.constant=0和subheaderHeightConstant.constant=0。我建议在底部使用>=约束,该约束修复了警告,并且仍然受到尊重。
- (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;
    }
}