anyName
anyName

Reputation: 113

UIImageView repeats showing in another cells

I dynamically add an UIImageView (as thumbnail) and its NSLayoutConstraints in my UIView which in the cell of a table. I have two problems about this.

  1. After an image view added, if the user inserts text to this table still an image is being shown. I mean, instead of text table prints one more image. However, if I stop and re-run the application, this time text is shown as it should be. Still, if I write a text picture is coming. Why and what should I do?

  2. I set image and its constraints like this:

    -(void)setThumbnail:(UIImageView)imageView {
    
    [self.messageView addSubview:imageView];
    
    NSLayoutConstraint *leadingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:8.f];
    NSLayoutConstraint *trailingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-8.f];
    NSLayoutConstraint *topOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:8.f];
    NSLayoutConstraint *bottomOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-8.f];
    [self.messageView addConstraint:leadingOfThumbnail];
    [self.messageView addConstraint:trailingOfThumbnail];
    [self.messageView addConstraint:topOfThumbnail];
    [self.messageView addConstraint:bottomOfThumbnail];
    [self.messageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    [self.messageView removeConstraints:[self.messageTextLabel constraints]];
    }
    

And while loading, I get a constraint error. It says:

2016-10-07 09:35:32.532 application[3733:2922397] Unable to simultaneously satisfy constraints.     Probably at least one of the constraints in the following list is one you don't want.   Try this:       (1) look at each constraint and try to figure out which you don't expect;       (2) find the code that added the unwanted constraint or constraints and fix it.     (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)  (
    "NSAutoresizingMaskLayoutConstraint:0x1281474d0 h=--& v=--& UIImageView:0x126d7e020.midY == + 100",
    "NSAutoresizingMaskLayoutConstraint:0x128147520 h=--& v=--& V:[UIImageView:0x126d7e020(200)]",
    "NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020]   (Names: '|':UIView:0x1281480d0 )" )

Will attempt to recover by breaking constraint  NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020]   (Names: '|':UIView:0x1281480d0 )

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

I checked, leading and top constrains set by me causes this error. Already they are not working correct. My image is 200 * 200, thus it does not need height and width constraint. I only want 8 pt leading, trailing, top, bottom from message view. What is the problem?

Thank you.

Explanation edit for first question: Table puts UIImageView randomly, multiple cells - not as it should be.

Upvotes: 0

Views: 104

Answers (3)

iGenio
iGenio

Reputation: 398

For the first issue, you can simply set cell image to nil after dequeing from tableview.

For the second issue, after ImageView initialization add this line, to avoid that autoresizingmask is translated to constraints:

imageView.translatesAutoresizingMaskIntoConstraints = false

Upvotes: 1

zajacd
zajacd

Reputation: 11

You may want to implement the prepareForReuse method and do some cleanup there, set the imageView to nil and remove it from the subview here.
This method gets called when your cell is dequeued and about to be reused in the tableview.

Upvotes: 1

Shayan Jalil
Shayan Jalil

Reputation: 588

I believe that your problem is because of the dequeCell functionality. UITbableView recycle cells. So if you place an image in one cell, there is a chance that it will appear in another cell.

To solve this, you need to place some conditions in your cellForRowAtIndexPath.

These conditions would check if you want text in your current cell or image or both.

If it only needs text, you need to remove the image from that cell.

If you still face problems, please share your cellForRowAtIndexPath code.

Upvotes: 0

Related Questions