Search code examples
objective-cuitableviewuiimagensurlrequest

Images from url in UITableViewCell loads only when i scroll the table view


Images downloaded from url displays image in UITableViewCell only when i start scrolling the UITableView. Now i am using following code to load image from url:

NSURL* url = [NSURL URLWithString:imageURL];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
        queue:[NSOperationQueue mainQueue]
        completionHandler:^(NSURLResponse * response,
            NSData * data,
            NSError * error) {
    if (!error){
            NSImage* image = [[NSImage alloc] initWithData:data];
        // do whatever you want with image
    }

}];

Thanks in advance.


Solution

  • You shouldn't be getting your images in - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath you should create a utility class that acts as backing datastore. As you code stands now you could conceivable be re-requesting the image multiple times as the cell is scrolled into and out of view.