-1

Summary of the problem

I have a UITableView in which I want to Collapse it's UIImageView cell part, whenever it doesn't return an image, as seen below

first image of tableview cell (the expected result)

The correct form second image of tableview cell (the no image result)

No image form

Background including what I've already tried

Below is the piece of code that I have been working on. Also notice that I'm using SDWebImage to get and set the image in the app (any method in tableview delegates, SDWebImage protocols etc that I'm missing just point me),

The comment out part is what I have done, i.e. used constraints to try the collapse of UIimageView not it messes up the whole tableView upon scrolling

Code

Here is piece of code I'm working on,

[cell.i_imageView sd_setImageWithURL:[NSURL URLWithString: item.PostimageUrl] placeholderImage:[UIImage imageNamed:@"no-image"] options:SDWebImageRefreshCached completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {

        if(!image)
        {
                    //cell.i_imageView.hidden = YES;

            NSLog(@"why no image des:%@",error);
            NSLog(@"why no image url:%@",imageURL);
        }

//            cell.imageHolderAspectConstraint.active = TRUE;
//            cell.imageHolderZeroImageConstraint.active = FALSE;
//        }
//        else
//        {
//            cell.imageHolderAspectConstraint.active = FALSE;
//            cell.imageHolderZeroImageConstraint.active = TRUE ;
//
//            [cell layoutIfNeeded];
//            [cell setNeedsLayout];
//        }

        [cell.i_imageView sd_removeActivityIndicator];

    }];

Any help in this matter is highly appreciated, cheers.

Edit

Here is the image of storyboard section, where do I need to embed stackview

enter image description here

enter image description here

Frostmourne
  • 156
  • 1
  • 19

4 Answers4

0

Add your ImageView in StackView. Now hide your ImageView when image is not available.

Note : UIStackView automatically handle height of cell.

Edit:

As of now your UI is something like this.

enter image description here

Now select your image holding view and click on embedded in and select UIStackView.

enter image description here

Now assign the same constraints that you have given to holding view to stackView.

Now your hierarchy is like below

enter image description here

Finally you need to do below code in CellForRow:

cell.vImageHoldingView.hidden = !image;
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0
  • Embed if in a stackView
  • Attach stackview anchors to the desired anchors
  • Hide image view if needed.

enter image description here

StackView automatically collapses its hidden arrangedSubViews

Note: If you set the image nil, it will collapse again, because stackView manages imageView height according to its image.

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
-2

If there is no image, make the ImageView height to 0. Remaining things will get handled if it has a proper constraints.

Harish
  • 537
  • 1
  • 4
  • 9
-2

make picture bottom constraint as outlet . Like this

var bottomViewConstraints: NSLayoutConstraint! 

and give value on collapse and expand. like this

bottomViewConstraints.constant = 0 // expand 
bottomViewConstraints.constant = 60 // collapse  
  • Perhaps, elaborate? – Frostmourne Aug 02 '19 at 10:38
  • You have image outlet , in your swift file . and in Xib file this image must have height Constratraint. var heightConstraints: NSLayoutConstraint! heightConstraints.constant = 0 // expand When you give constant = 0 if will hide your image view . (Which Seem like your requirments) heightConstraints.constant = 60 // collapse – Humz Shahid Aug 04 '19 at 07:48