首先,系统自动删除正确的约束,以便一切按预期工作,但我想了解如何使我的代码更好。 Uitableview正在iOS 8.0自动模式下工作,并没有实现heightforindexpath方法。
uitableview在uitableviewcells中显示图像,我只是想在uiimageview中添加一个比率约束,以便正确显示图像。它似乎按预期工作,但可能有一些我不理解的东西(方法的顺序或约束没有在适当的时候给出我不确定)。
以下是您可以查看的代码的一部分。图像宽度和高度存储在数据库中,用于在下载图像之前设置约束。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PostImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierPostImage];
if (cell == nil) {
cell = [[PostImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierPostImage]; }
// Adding image
[cell.imagePost setImageWithURL:[NSURL URLWithString:[[self.myData objectAtIndex:indexPath.row] valueForKey:@"image"]] placeholderImage:[UIImage imageNamed:@""]];
// Adding constraint
[cell setConstraints:[[[self.myData objectAtIndex:indexPath.row] valueForKey:@"imageWidth"] doubleValue] withHeight:[[[self.myData objectAtIndex:indexPath.row] valueForKey:@"imageHeight"] doubleValue]];
return cell;
}
PostImageTableViewCell自定义类的setConstraints方法在这里。我想要实现的是删除给前一个单元格的旧约束,并在下一个单元格中为新图像添加新约束...但是根据我从日志中理解它似乎不会那样做。是混合约束还是我必须给比率数字更清洁?
- (void)setConstraints:(double)width withHeight:(double)height {
int fullWidth = [[UIScreen mainScreen] applicationFrame].size.width;
[self.imagePost removeConstraint:self.imageConstraint];
self.imageConstraint = [NSLayoutConstraint constraintWithItem:self.imagePost attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.imagePost attribute:NSLayoutAttributeHeight multiplier:(MAX(width,(fullWidth-20))/height) constant:0.0];
[self.imagePost addConstraint:self.imageConstraint];
}
错误讯息。它似乎不喜欢给出的比率约束......再次,它在模拟器/设备中正确显示,但我想了解我做错了什么。
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fb80cbac720 V:|-(11.5)-[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39'] (Names: '|':UITableViewCellContentView:0x7fb80cb8bfd0 )>",
"<NSLayoutConstraint:0x7fb80cbac7c0 V:[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39'(23)]>",
"<NSLayoutConstraint:0x7fb80cbac580 V:[CustLabelWithPadding:0x7fb80cb66240'JUL 20 - 18:39']-(11.5)-[CustLabelWithPadding:0x7fb80b90d8e0'Tr']>",
"<NSLayoutConstraint:0x7fb80cbac8c0 V:[CustLabelWithPadding:0x7fb80b90d8e0'Tr'(>=34.5)]>",
"<NSLayoutConstraint:0x7fb80cbac840 V:[CustLabelWithPadding:0x7fb80b90d8e0'Tr']-(1)-[CustLabelWithPadding:0x7fb80cba83e0'Rr']>",
"<NSLayoutConstraint:0x7fb80cbac9e0 V:[CustLabelWithPadding:0x7fb80cba83e0'Rr'(>=34.5)]>",
"<NSLayoutConstraint:0x7fb80cbac910 V:[CustLabelWithPadding:0x7fb80cba83e0'Rr']-(1)-[UIImageView:0x7fb80cba8a20]>",
"<NSLayoutConstraint:0x7fb80cbaca30 V:[UIImageView:0x7fb80cba8a20]-(1)-[UIView:0x7fb80cba8c20]>",
"<NSLayoutConstraint:0x7fb80cbacc00 V:[UIView:0x7fb80cba8c20(50.6)]>",
"<NSLayoutConstraint:0x7fb80cbacb80 V:[UIView:0x7fb80cba8c20]-(11.5)-| (Names: '|':UITableViewCellContentView:0x7fb80cb8bfd0 )>",
"<NSLayoutConstraint:0x7fb80cbb0130 H:[UIImageView:0x7fb80cba8a20(352)]>",
"<NSLayoutConstraint:0x7fb80cbb2080 UIImageView:0x7fb80cba8a20.width == 1.65647*UIImageView:0x7fb80cba8a20.height>",
"<NSLayoutConstraint:0x7fb80cbabf60 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fb80cb8bfd0(392.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb80cbb2080 UIImageView:0x7fb80cba8a20.width == 1.65647*UIImageView:0x7fb80cba8a20.height>
我清理了一下代码,但基本上可用的宽度是375-20 * 1.15。图像高度为212.5(存储在数据库中的数字),因此最终比率为352 / 212.5 = 1.65647 ...如错误消息中所示。另一个约束工作正常,我认为它们用于没有图像的单元格,我没有错误信息。
答案 0 :(得分:1)
实际上,如果你尝试进行数学计算,我们最终会错过0.1 ...最小刻度是半个点,系统最终找不到相同的高度总和。我用高度约束替换了比率约束,并确保我给出了一个合适的高度(没有&#34;奇怪的数字&#34;)
[self.contentView removeConstraints:self.imageConstraints];
double ratio = (MAX(width,(fullWidth-20*multiplier))/height);
double newHeight = floor((fullWidth / ratio) / 0.5 + 1) * 0.5;
NSDictionary *viewsDic = @{@"imagePost":self.imagePost};
self.imageConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:[imagePost(%f)]",newHeight] options:0 metrics:0 views:viewsDic];
[self.contentView addConstraints:self.imageConstraints];
正如你所看到的那样,newHeight被修改了,所以我们最终得到了一个很好的&#34;系统会喜欢的号码。这删除了我所有的约束问题。实际上,50.6的高度也是如此。使用相同的等式,我确保发送51而不是50.6。