How to fix a slow scrolling table view

你说的曾经没有我的故事 提交于 2019-12-23 05:04:05

问题


I have a table view that's scrolling slowly. Does anyone know why that might be?

There is an image for each row, but even after the images are loaded it still stutters and scrolls slowly.

thanks for any help

here's my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    // Get item from tableData
    NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];

    // display the youdeal deal image
    photoString = [item objectForKey:@"image"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];

    cell.titleLabel.text = [item objectForKey:@"supercat"];
    cell.descriptionLabel.text = [item objectForKey:@"title"];

    NSString *convertedLeftCount = [NSString stringWithFormat:@"%@",[item objectForKey:@"left_count"]];

    cell.amountLabel.text = convertedLeftCount;

    cell.thumbnailImageView.image = image;
    cell.priceLabel.text = [item objectForKey:@"cat"];

    return cell;
}

回答1:


It's due to the image loading mechanism you used.

You are loading the image from url in the main thread. That's why the UI is blocked for some time also the dataWithContentsOfURL: is a synchronous call. So the UI will respond after getting the image data.

Apple states that the time taking processes like webrequest,parsing huge data etc must be done on other threads rather than main thread. And all UI related tasks must be done on main thread.

Solutions:

  1. Request the image in background thread, not in main thread.
  2. Cache the image once you get it

Source code and Third Party Libraries

Here are some links which will help you to understand the basic idea of loaing image using asynchronous methods

  1. LazyTableImages
  2. HJCache
  3. SDWebImage



回答2:


The images are getting loaded every time a cell is loaded, because the imageWithData: doesn't use any cache.

Edit: I saw a comment that suggests loading images asynchronously. You already have your custom class for each cell so it should be easy to do it. If it were an answer I'd vote it up




回答3:


I think You are trying to say this.

NSURL* url = [NSURL URLWithString:@"http://www.YourImageUrl.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];


[NSURLConnection sendAsynchronousRequest:request
    queue:[NSOperationQueue mainQueue]
    completionHandler:^(NSURLResponse * response,
        NSData * data,
        NSError * error) {
if (!error){
        UIImage* image = [[UIImage alloc] initWithData:data];
    // Now workout with the image
}

}];

This will make the asynchronous call, and load the images after tableview loaded i.e when the image load complete the image will show but the table will be loaded when the table view is needed to load.



来源:https://stackoverflow.com/questions/13533244/how-to-fix-a-slow-scrolling-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!