2

I'm using SDWebImage. I'm pulling the images in correctly from a web service API, but if the API I'm getting the response from doesn't have an image ("null"), I want to realign my Table View Cell.

View Controller.m

[cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder"]];

WebListCell.m

- (void)layoutSubviews {
    [super layoutSubviews];
    self.headlineLabel.frame = CGRectMake(134, 12.5, 130, 50);
    self.descriptionLabel.frame = CGRectMake(134, 65, 130, 50);
    self.imageView.frame = CGRectMake(12, 15, 96, 54);

    //This Part Not Working
    float limgW =  self.imageView.image.size.width;
    if (limgW == 1) {
        self.headlineLabel.frame = CGRectMake(15, 15, 250, 50);
        self.descriptionLabel.frame = CGRectMake(15, 65, 250, 50);
        self.imageView.frame = CGRectMake(2, 2, 2, 2);
    }
}

I was using this as a general guide: http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/

(My placeholder image right now is just 1px by 1px)

So basically my problem is I can't find a good "if" statement for when there is no image and I want to realign my Table View Cell.

Any advice on an easy "if" statement?

EDIT: Using this code now, which works, except I'm getting a warning that says "Capturing cell strongly in this block is likely to lead to a retain cycle"

[cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              if(image == nil) {
                                  //realign your table view cell
                                  [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://website.com/image1"]
                                                 placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                                   ];
                              }
                          }];
Realinstomp
  • 532
  • 2
  • 13
  • 30
  • Why are you comparing against 1? – Carl Veazey May 23 '13 at 03:10
  • @CarlVeazey I was following the tutorial I linked to, even though its not an apples to apples fit for that specific part. So that part of the code is wrong, I tried to mark it that way, because I figured it'd be better to post all I had come up with for it than nothing. My original idea was to check if it was the placeholder image (1px by 1px) but i realized that was incorrect to do. Thanks for checking on that though- – Realinstomp May 24 '13 at 00:07

1 Answers1

3

Try using the block to check whether the image retrieval is successful or not. And also added the weak reference to cell: Fix warning "Capturing [an object] strongly in this block is likely to lead to a retain cycle" in ARC-enabled code

From the SDWebImage github page:

With blocks, you can be notified about the image download progress and whenever the image retrival has completed with success or not:

// Here we use the new provided setImageWithURL: method to load the web image
__weak UITableViewCell *wcell = cell;
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
                placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
                       completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    if(image == nil) {
        //realign your table view cell
        [wcell.imageView setImageWithURL:[NSURL URLWithString:@"http://website.com/image1"]
                                             placeholderImage:[UIImage imageNamed:@"placeholder.png"]
        ];
    }
}];
Community
  • 1
  • 1
Valent Richie
  • 5,226
  • 1
  • 20
  • 21
  • Thanks for the response! This looks like the sort of thing I'm looking for. I'm newer to this, so what type of "completion code" would you suggest? – Realinstomp May 24 '13 at 00:05
  • @Reez updated my answer. You can also check for the error variable for more robust checking if you need to. – Valent Richie May 24 '13 at 00:53
  • Awesome, this looks great. Quick question though, right now I am styling the current cell separately in a subclass called `WebListCell`, so should I move that code into this code you provided? I see that You have a spot for me to realign the table view cell, and just am not sure if I should be aligning the table view cell there for one condition and aligning the table view cell in the `WebListCell` for the different condition. What do you think? – Realinstomp May 25 '13 at 21:00
  • @Reez I think it really depends. If you will reuse the aligning in the WebListCell for one condition, I think you can just leave it there. – Valent Richie May 26 '13 at 15:39
  • This is working great for me now, except the warning I'm getting (showed in "edit" code above), which shows up on the first line of the code inside the block... any ideas on getting that to not show up? Thanks again- – Realinstomp May 29 '13 at 21:13
  • In playing around with it, if I were to just not put anything in the `//realign your table view cell` then the images do this weird thing where they don't load until their offscreen and I've scrolled past them. Just another bit of info while I was trying to track down the problem, because I realized I might not need to be putting `cell.imageView` in that part potentially – Realinstomp May 29 '13 at 21:32
  • It is still waiting to load images until I start scrolling up and down the list. As soon as the `ViewController` loads, everything else in the `TableView` loads except for the `Image`. The `Image`s finally start to appear only once I start scrolling, do you know why its doing that? – Realinstomp May 30 '13 at 16:56
  • Or it takes me holding down the `TableViewCell` to make the image appear – Realinstomp May 30 '13 at 17:00
  • Also, do I need to refresh the TableView somehow, because it won't show API images until I've scrolled on the Table? – Realinstomp Jul 04 '13 at 20:09