我有一个客户UITableViewCell。它包含从上到下的标签和UIImageViews。我为标签和UIImageViews添加了约束。所以我可以使用systemLayoutSizeFittingSize:UILayoutFittingCompressedSize来计算适合内容的单元格高度。但是UIImageView中的图像是使用SDWebImage从在线加载的。起初我可以为UIImageView设置一些约束。加载图像时,我应该更改UIImageView的约束以满足其大小。但有时会发生错误。有时它没关系:
*断言失败 - [NSLayoutConstraint _setSymbolicConstant:constant:],/ SourceCache /Foundation_Sim / Foundation-1142.14 / Layout.subproj / NSLayoutConstraint.m:585 2015-04-01 19:57:44.537 HiPDA [3760:63583]
* 由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'常量不是有限的!这是非法的。常数:楠'
***首先抛出调用堆栈:
(
0 CoreFoundation 0x00000001113cfa75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000111068bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001113cf8da + [NSException raise:format:arguments:] + 106
3基础0x000000010ef0cb6f - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4基金会0x000000010ee87c7d - [NSLayoutConstraint _setSymbolicConstant:constant:] + 149
代码是:
__weak typeof(self) weakSelf = self;
__weak typeof(UIImageView *) weakImgView=imgView;
[imgView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:THREADLISTDETAILIMAGE]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
CGFloat width=image.size.width>maxWidth?maxWidth:image.size.width;
NSLayoutConstraint *imgW1=[NSLayoutConstraint constraintWithItem:weakImgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:0.0 constant:width];
[weakSelf.contentView addConstraint:imgW1];
NSLayoutConstraint *imgH1=[NSLayoutConstraint constraintWithItem:weakImgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:0.0 constant:width*image.size.height/image.size.width];
[weakSelf.contentView addConstraint:imgH1];
[weakSelf needsUpdateConstraints];
[weakSelf layoutIfNeeded];
[weakSelf layoutSubviews];
}];
如果完全下载图像,则表格视图不会刷新。我可以看到图像来自单元格大小。我该如何解决?
答案 0 :(得分:2)
你必须为imageView取出Outlet NSLayoutConstraint,并以编程方式更改值
self.imgViewHeightConstraint.constant = 300(某个值)