在下面的代码中,调用这四种方法进行布局推理.我有点困惑为什么所有这些都是需要的,以及它们彼此不同的做法.它们在过程中用于通过自动布局使单元格的高度变为动态.(来自该库从这个问题.)
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
它来自这个单元格高度的代码块:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
RJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell updateFonts];
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
cell.bodyLabel.text = [dataSourceItem valueForKey:@"body"];
cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f);
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
Run Code Online (Sandbox Code Playgroud)
但他们的做法有何不同?他们为什么都需要?
很抱歉打扰但我正在尝试延迟加载几个imageViews,然后按比例调整它与UITableView中的内容.我也在尝试(Unwisely或许?)第一次使用Autolayout.我不明白为什么约束在这种情况下不起作用.这是在我将正确的图像加载到UIImageView之后用来调整UIImageView大小的代码.
// Scale the image view and return it.
- (UIImageView *) scaleImageViewForScreenWidth:(UIImageView *)imageView {
UIImage *imgFromView = [imageView image];
CGRect newFrame = CGRectMake(0, 0, imgFromView.size.width, imgFromView.size.height);
float imgFactor = newFrame.size.height / newFrame.size.width;
newFrame.size.width = [[UIScreen mainScreen] bounds].size.width;
newFrame.size.height = newFrame.size.width * imgFactor;
[imageView setFrame:newFrame];
return imageView;
}
Run Code Online (Sandbox Code Playgroud)
就约束而言.我正在尝试将带有阴影的Label和UIImageView附加到主imageview的底部.以下是我在imageview中应用于底部阴影的约束.底部阴影约束是:
Height Equals: 83
Align Bottom to: Background Image View
LeadingSpace to: Table View Cell
Run Code Online (Sandbox Code Playgroud)
我没有得到我想要的东西.有任何想法吗?我觉得我在和autolayout战斗.