Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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 无法同时满足UITableView中的约束_Ios_Uitableview_Nslayoutconstraint - Fatal编程技术网

Ios 无法同时满足UITableView中的约束

Ios 无法同时满足UITableView中的约束,ios,uitableview,nslayoutconstraint,Ios,Uitableview,Nslayoutconstraint,在我看来 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"cell"; LZHThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [

在我看来

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
LZHThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
    cell = [[LZHThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}

[cell configureThread:self.threads[indexPath.row]];

return cell;
}
在UITableViewCell中

-(id)configureThread:(LZHThread *)thread{
[_avatarImageView sd_setImageWithURL:thread.user.avatarImageURL];
_avatarImageView.translatesAutoresizingMaskIntoConstraints=NO;
_userNameLabel.text=thread.user.userName;
[_userNameLabel sizeToFit];
_userNameLabel.translatesAutoresizingMaskIntoConstraints=NO;
_countLabel.text=[NSString stringWithFormat:@"%ld/%ld",thread.replyCount,thread.totalCount];
[_countLabel sizeToFit];
_countLabel.translatesAutoresizingMaskIntoConstraints=NO;
_postTimeLabel.text=thread.postTime;
[_postTimeLabel sizeToFit];
_postTimeLabel.translatesAutoresizingMaskIntoConstraints=NO;
_titleLabel.text=thread.title;
_titleLabel.numberOfLines=0;
_titleLabel.lineBreakMode=NSLineBreakByCharWrapping;
CGSize optSize=[_titleLabel sizeThatFits:CGSizeMake(self.contentView.frame.size.width-2*kDistanceBetweenViews, 99999)];
_titleLabel.frame=CGRectMake(0, 0, optSize.width, optSize.height);
_titleLabel.translatesAutoresizingMaskIntoConstraints=NO;
[self configureViewConstrains];
return self;
}
我使用以下方法设置约束

-(void)configureViewConstrains{
//_avatarImageView
NSLayoutConstraint *avatarConstrainX=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *avatarConstrainY=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *avatarConstrainWidth=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:kAvatarImageViewSize];
NSLayoutConstraint *avatarConstrainHeight=[NSLayoutConstraint constraintWithItem:_avatarImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:kAvatarImageViewSize];
[self.contentView addConstraints:@[avatarConstrainX,avatarConstrainY,avatarConstrainWidth,avatarConstrainHeight]];

//_userNameLabel
NSLayoutConstraint *userNameConstrainX=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *userNameConstrainY=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *userNameConstrainWidth=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_userNameLabel.frame.size.width];
NSLayoutConstraint *userNameConstrainHeight=[NSLayoutConstraint constraintWithItem:_userNameLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_userNameLabel.frame.size.height];
[self.contentView addConstraints:@[userNameConstrainX,userNameConstrainY,userNameConstrainWidth,userNameConstrainHeight]];

//_postTimeLabel
NSLayoutConstraint *postTimeConstrainX=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-kDistanceBetweenViews-_postTimeLabel.frame.size.width];
NSLayoutConstraint *postTimeConstrainY=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *postTimeConstrainWidth=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_postTimeLabel.frame.size.width];
NSLayoutConstraint *postTimeConstrainHeight=[NSLayoutConstraint constraintWithItem:_postTimeLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_postTimeLabel.frame.size.height];
[self.contentView addConstraints:@[postTimeConstrainX,postTimeConstrainY,postTimeConstrainWidth,postTimeConstrainHeight]];

//_countLabel
NSLayoutConstraint *countConstrainX=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_postTimeLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-kDistanceBetweenViews-_countLabel.frame.size.width];
NSLayoutConstraint *countConstrainY=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
NSLayoutConstraint *countConstrainWidth=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_countLabel.frame.size.width];
NSLayoutConstraint *countConstrainHeight=[NSLayoutConstraint constraintWithItem:_countLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_countLabel.frame.size.height];
[self.contentView addConstraints:@[countConstrainX,countConstrainY,countConstrainWidth,countConstrainHeight]];

//_titleLabel
NSLayoutConstraint *titleConstrainX=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *titleConstrainY=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_avatarImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:kDistanceBetweenViews];
NSLayoutConstraint *titleConstrainWidth=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.0 constant:_titleLabel.frame.size.width];
NSLayoutConstraint *titleConstrainHeight=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.0 constant:_titleLabel.frame.size.height];
NSLayoutConstraint *titleConstrainBottom=[NSLayoutConstraint constraintWithItem:_titleLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-kDistanceBetweenViews];
[self.contentView addConstraints:@[titleConstrainX,titleConstrainY,titleConstrainWidth,titleConstrainHeight,titleConstrainBottom]];
 }
然后在UtableView中,我得到了高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
LZHThreadTableViewCell *cell=[[LZHThreadTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
[cell configureThread:self.threads[indexPath.row]];
CGSize optSize=[cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
return optSize.height;
}
但也有警告发生

"<NSLayoutConstraint:0x7ff7a96cfd90 V:|-(8)-[UIImageView:0x7ff7a96a9390]   (Names: '|':UITableViewCellContentView:0x7ff7a96b6e00 )>",
"<NSLayoutConstraint:0x7ff7a96c1260 V:[UIImageView:0x7ff7a96a9390(34)]>",
"<NSLayoutConstraint:0x7ff7a96dcb20 V:[UIImageView:0x7ff7a96a9390]-(8)-[UILabel:0x7ff7a96afa10'0\Ud83d\Udcce']>",
"<NSLayoutConstraint:0x7ff7a96dcbf0 V:[UILabel:0x7ff7a96afa10'0\Ud83d\Udcce'(20.5)]>",
"<NSLayoutConstraint:0x7ff7a96c3870 UILabel:0x7ff7a96afa10'0\Ud83d\Udcce'.bottom == UITableViewCellContentView:0x7ff7a96b6e00.bottom - 8>",
"<NSLayoutConstraint:0x7ff7a958e4c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff7a96b6e00(78)]>"
“”,
"",
"",
"",
"",
""
)

将尝试通过打破约束进行恢复


我不知道发生了什么?

看起来好像是说您给出的高度限制相互冲突。从上面我可以看到,您有:

  • 8像素空间
  • 然后是34像素的图像视图
  • 然后是另一个8像素的空间,然后是一个20.5像素高的标签
  • 您说过该标签的底部距离单元格内容视图的底部8像素
  • 让我们把这些数字加起来:

    8+34+8+20.5=70.5像素高

    但是,您似乎特别指出标签的底部需要与
    UITableViewCell
    的底部相距8像素,后者为78像素

    如果你从78个像素减去8个像素,那么你会有70个像素高,这和我们刚刚加起来的70.5个像素相冲突


    您可能需要再次检查约束:D

    我知道发生了什么。如果我将分隔符样式设置为默认值,则分隔符行高为1.0,因此单元格的内容高度为0.5。如果我将分隔线设置为“无”。或者只需在indext path的行的方法高度中添加0.5高度。它将很好地工作。