0

I want to load an image onto a table view cell, i.e., a custom cell with an image view. And, I use SDWebImage. I am loading an image onto the cell without using setImageWithURL. This is the code inside cellForRowAtIndexPath.

[_imgManager downloadWithURL:urlArray[indexPath.row] 
                   completed:^(UIImage *image, NSError *error//yada, yada) {

    if(image)
    {
        NSLog(@"Image received");

        //cell.pictureView.image = image; // doesn't work, so I did 

        dispatch_async(dispatch_get_main_queue(), ^{

            UITableViewCell *tCell = [self.tableName cellForRowAtIndexPath:indexPath];
            if(tCell)
                tCell.imageView.image = image;

        }); // doesn't work either
    }
}];

So as I have mentioned in the comments, it doesn't work. What am I doing wrong? Or maybe my conception of this is wrong? The images load only after I scroll (that activates cellForRowAtIndexPath for other cells). And they keep refreshing on each appearance. It doesn't work exactly as expected.

esh
  • 2,842
  • 5
  • 23
  • 39
  • Maybe my answer is [here](http://stackoverflow.com/questions/17083629/sdwebimage-uitableview-cell-images-not-persisting?rq=1). – esh Jul 04 '13 at 15:27
  • Why don't you want to use `setImageWithURL` as it takes care of this scenario far more gracefully (and handles scenarios that yours won't, namely if the cell is reused, your solution won't cancel backlogged requests resulting in a slower UI)? – Rob Jul 04 '13 at 15:27
  • `setImageWithURL` cancels other downloads on the cell. I am using a prefetcher as well, so it looks like both of these are fighting to download the same thing. So I tried to fend off the prefetcher from loading the current cell. But it seems like the use-case is so indeterminate that the cell could change any time. So I should be able to allow only **one** thing to happen. And `setImageWithURL` all by itself is perceived slow. – esh Jul 04 '13 at 15:29
  • Yes, but isn't it good to cancel the other downloads? If you start at the start of the table and the user scrolls down to the bottom, the images at the bottom (i.e. what the user is looking at and cares about) won't show up until the other requests finish. It's not much of an issue on fast network, but on slow network with quick scroll, you'll see delay before images show up for the visible portion of the table. I would have thought the user would like to see the images on visible portion more quickly, rather than worrying that if they scroll back up, those other images are already retrieved. – Rob Jul 04 '13 at 15:32
  • But setting all of that aside, you say "it doesn't work." What happens? Do you not see "Image received"? If nothing else, you should have an `else` clause for your `if` statement that logs `error`. If you don't look at the `NSError`, you're flying blind. – Rob Jul 04 '13 at 15:37
  • I didn't put that up here. I have logged error as well. And there are no errors to speak of. It just looks like throttling or something. I just want to download it myself without bothering about `setImageWithURL` waiting to cancel whatever is already running for that cell. – esh Jul 04 '13 at 15:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32899/discussion-between-blackflam3-and-rob) – esh Jul 04 '13 at 15:41

0 Answers0