0

I have a table view that loads title and images from url address. Since I added the images, the scrolling isn't smooth. I have changed the background to clear. The images are low resolution. I prefer accessing them with url, not to downloaded the image and re-upload it on the app. Looking forward for your solutions to make the scrolling smooth. Thanks

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell" forIndexPath:indexPath];
NSString *str = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setFont:[UIFont systemFontOfSize: 16.0]];
cell.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor clearColor];

cell.TitleLabel.text=str;

UIImage *pImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:feeds2[indexPath.row]]]];;
[cell.ThumbImage setImage:pImage];

return cell;

}

1
  • 1
    load image on background thread , not on main thread. Commented Jun 8, 2014 at 19:33

2 Answers 2

1

replace your code in cellForRowAtIndexPath:

after this line cell.TitleLabel.text=str;

That way you load each image in the background and as soon as its loaded the corresponding cell is updated on the mainThread.

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
        NSData *imgData =  NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:feeds2[indexPath.row]]];
        if (imgData) {
           UIImage *image = [UIImage imageWithData:imgData];

           dispatch_sync(dispatch_get_main_queue(), ^(void) {
             UIImage *image = [UIImage imageWithData:imgData];
                      if (image) {
                           cell.ThumbImage.image = image;
             }
        });
    });

A better approach is to cache the image , so you dont need to download them each time , the table scroll.

here are some very good references to accomplish this.

LazyTableImages Reference

SDWebImage

UIImageView+AFNetworking

Sign up to request clarification or add additional context in comments.

Comments

1

Answer is simply you have to implement the loading with NSOperation where a custom class to handle your download and have your NSOperationQueue as downloadQueue. Every UITableView is a (sub class) UIScrollView therefore you can use the methods directly.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    [downloadQueue cancelAllOperations]; // clear your queue
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if (!decelerate) {
        // start download only for visible cells
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    // start download only for visible cells
}

for more detail information visit this tutorial. There you can really find good solution for your need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.