I'm having trouble with handling constraints within TableViewCell. I have an imageview in the cell and I wanted to update the constraint of the image. For instance, the default value of the width constraint is 120. If the imageUrl is not empty, the width should be 120, else it will be 0 width.
I used the code below to change it to 0 and it worked perfectly. After I scroll around the TableView, most of my images went missing due to the constraint lock it at 0 width. I wanted to update it back to 120 width, but it doesn't seem to work. Any help is appreciated.
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];
}