【发布时间】:2015-08-13 03:47:48
【问题描述】:
我在处理TableViewCell 中的约束时遇到问题。我在 cell 中有一个 imageview,我想更新图像的约束。例如,宽度约束的默认值为120。如果imageUrl不为空,则宽度应为120,否则为0宽度。
我使用下面的代码将其更改为 0,它运行良好。在我滚动 TableView 后,由于约束将其锁定在 0 宽度,我的大部分图像都丢失了。我想将它更新回 120 宽度,但它似乎不起作用。任何帮助表示赞赏。
CGFloat cellImageWidth = 120.0;
NSDictionary *mainDictionary = @{@"cellImage":cellImage};
NSArray *zeroWidth = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[cellImage(%f)]", 0.0]
options:0
metrics:nil
views:mainDictionary];
NSArray *normalWidth = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[cellImage(%f)]", cellImageWidth]
options:0
metrics:nil
views:mainDictionary];
if(![[imgArr objectAtIndex:indexPath.row] isEqualToString:@""]){
[cellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", imageInbox, [imgArr objectAtIndex:indexPath.row]]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cellImage.hidden = NO;
[cellImage removeConstraints:zeroWidth];
[cellImage addConstraints:normalWidth];
[cellImage updateConstraints];
}else{
cellImage.hidden = YES;
[cellImage removeConstraints:normalWidth];
[cellImage addConstraints:zeroWidth];
[cellImage updateConstraints];
}
【问题讨论】:
标签: ios uitableview autolayout constraints tableviewcell