0

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];

    }

2 Answers 2

1

Think about it:

  1. Why not update the used constraint? Set constraint once and if you want to update change constant property.
  2. you really needs change constraint to zero? Other constrains depends of it?
  3. Override prepareForReuse. This method is called when a UITableViewCell is prepared to be reused to display information for another object in the UITableView, which incidentally happens when scrolling occurs. The solution was as simple as inserting the following code into my UITableViewCell subclass: -(void) prepareForReuse { [self setNeedsUpdateConstraints]; }
Sign up to request clarification or add additional context in comments.

3 Comments

1. I tried using constant property. But within tableviewcell it doesn't allow this method. This will cause error during compile. 2. Yes, other constraint depends on it. 3. Does this means that I should create a custom tableviewcell with prepareForReuse method?
3. prepareForReuse is part of UITableViewCell life cycle: developer.apple.com/library/ios/documentation/UIKit/Reference/…
2. If you´re getting error change constant of a Constraint your are doing it wrong. Have you got XIB or storboard cell for this UITableViewCell? If yes, connect the constraints you have setup in xib/storyboard to an outlet in the code, and modify its constant value. If not, where are you creating this constraint (method)?
0

Try to use this.

NSLayoutConstraint *myConstraint =[NSLayoutConstraint 
                 constraintWithItem:Your_Image
                 attribute:NSLayoutAttributeWidth
                 relatedBy:NSLayoutRelationGreaterThanOrEqual
                 toItem:nil
                 attribute:NSLayoutAttributeWidth
                 multiplier:1
                 constant:120];//120 is the width you want 

5 Comments

I tried this method.. Sadly the result is the same. It's not updating back to 120
can you try by creating outlet of width constraint & then set its constant to 120.
It seems that xcode doesn't allow matching IBOutlet with the content of tableviewCell. I'm not sure whether there is a workaround for this. Any idea?
You have to add constraint from storyboard. then go to width constraint of your image & then drag drop to your .h file it will crate the outlet of width constraint. & then write the code as YOUR_OUTLET_NAME.constant=120
Yes. I did the same way as you mentioned. During compilation it occurs error on that constraint. It doesn't allow to compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.