2015-04-01 101 views
1

我有一个custome UITableViewCell。它包含从上到下的标签和UIImageView。我为标签和UIImageView添加了约束。所以我可以使用systemLayoutSizeFittingSize:UILayoutFittingCompressedSize来计算适合内容的单元格高度。但是UIImageView中的图像是使用SDWebImage在线加载的。起初我可以为UIImageView设置一些约束。当图像加载时,我应该改变UIImageView的约束来满足它的大小。但有时会发生错误。有时,它的确定:如何动态设置UIView约束?

*在断言失败 - [NSLayoutConstraint _setSymbolicConstant:常数:],/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提高:格式:参数:] + 106
3基础0x000000010ef0cb6f - [NSAssertionHandler handleFailureInMethod:对象:文件:LINENUMBER:描述:] + 195
4基金会0x000000010ee87c7d - [NSLayoutConstraint _setSymbolicConstant:常数:] + 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]; 
}]; 

如果图像完全下载后,tableview中不会刷新。我可以看到图像出来的单元格大小。我该如何解决这个问题?

回答

2

你必须采取出口NSLayoutConstraint为ImageView的编程和您可以更改像

self.imgViewHeightConstraint.constant = 300的值(一定值)

+1

和加载图像时,如何调用tableview刷新。第一张图片显示何时加载图片:http://imgsrc.baidu.com/forum/w%3D580/sign=a7990bbd69224f4a5799731b39f69044/70fea018367adab45f8f66d88fd4b31c8601e431.jpg第二次手动刷新:http://imgsrc.baidu.com/forum/w% 3D580/sign = eef4736c9b3df8dca63d8f99fd1072bf/1c09d61f3a292df57ec5d18eb8315c6035a8730a.jpg – leizh00701 2015-04-01 12:40:31

+1

为了让整个tableview刷新你可以调用'[tableview reloadData]'或为单个单元重新加载,如果你知道你想要哪个indexPath,你可以调用'reloadRowsAtIndexPaths:withRowAnimation:'方法刷新。 – 2015-04-01 12:45:12