0

I have multiple sections, there are 10 rows on each there. But, they do not move smoothly when scrolling the table. They stop sometimes for second. Can anyone help me figure out why?

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"CustumCell";
        CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if ([tableDataArray count] == 0) {
        } else {
            NSInteger index = indexPath.section * 10 + indexPath.row;
            NSString * newsValue =[[tableDataArray objectAtIndex:index]objectForKey:@"NewsValue"];
            NSDictionary * newsDict =(NSDictionary *)[newsValue JSONValue];


            cell.newsTitleLabel.text = [[tableDataArray objectAtIndex:index]objectForKey:@"Title"];

            UIImage *image;
            NSString *userString =[newsDict objectForKey:@"imgUrl"];


            if([[ImageCache sharedImageCache] DoesExist:userString] == true) {
                image = [[ImageCache sharedImageCache] GetImage:userString];

                cell.newsImage.image = [ImageResizer squareImageTo:image :CGSizeMake(75, 75)];
            } else {
                NSURL *url = [NSURL URLWithString:userString];


                NSURLSessionTask *task8 = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                    if (data) {

                        UIImage *image = [UIImage imageWithData:data];

                        if (image) {

                            dispatch_async(dispatch_get_main_queue(), ^{

                                CustumCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];

                                if (updateCell)

                                                                 updateCell.newsImage.image = [ImageResizer squareImageTo:image :CGSizeMake(75, 75)];

                                [[ImageCache sharedImageCache] AddImage:userString :image];

                            });

                        }

                    }

                }];
                [task8 resume];
                        }

        }
        return cell;
    }
Jason Roman
  • 8,146
  • 10
  • 35
  • 40

2 Answers2

0

You seem to be downloading the image and resizing it on the main thread which is should only be used for UI stuff.

You should move your downloading to a background thread and should also move the resizing code to background thread.

Refer this Lazy load images in UITableView to understand how lazy loading works.

Community
  • 1
  • 1
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • I am not here to code for you if that is what you are looking for. I have guided you , rest you will have to find on your own. Go through apple docs and lot of ray wenderlich tutorials before you start implementing code or else you will spoil the code base. – Ankit Srivastava Jan 15 '16 at 06:12
0

Managing the download of images to be displayed in a UITableView is very tricky. With a naive implementation, tens of downloads for images that are no longer in the visible part of the table view can be started if the user scrolls quickly. And the images can be inserted into the wrong table view row since table view rows are recycled.

I therefore strongly suggest to use an image cache built for this purpose, e.g. SDWebImage.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • I am using SDWebImage but on using SDWebImage some image not uploading. I am using this line – varun malik Jan 15 '16 at 07:21
  • [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; – varun malik Jan 15 '16 at 07:21
  • "uploading"? You probably mean downloading. I can't really help you here without further information. But you better find the issue with SDWebImage and fix it than writing a new cache implementation yourself. – Codo Jan 15 '16 at 07:31
  • sorry its downloading – varun malik Jan 15 '16 at 07:33
  • Why don't you open a new SO question describing your issue with SDWebImage? – Codo Jan 15 '16 at 07:35