简体   繁体   English

如何修复慢速滚动表视图

[英]How to fix a slow scrolling table view

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;
}

It's due to the image loading mechanism you used. 这是由于您使用了图像加载机制。

You are loading the image from url in the main thread. 您正在从主线程中的url加载图像。 That's why the UI is blocked for some time also the dataWithContentsOfURL: is a synchronous call. 这就是为什么UI被阻塞一段时间的原因,同时dataWithContentsOfURL:是一个同步调用。 So the UI will respond after getting the image data. 因此,UI将在获取图像数据后做出响应。

Apple states that the time taking processes like webrequest,parsing huge data etc must be done on other threads rather than main thread. 苹果公司指出,诸如webrequest,解析大量数据之类的耗时过程必须在其他线程而不是主线程上完成。 And all UI related tasks must be done on main thread. 并且所有与UI相关的任务都必须在主线程上完成。

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 LazyTableImages
  2. HJCache HJCache
  3. SDWebImage SDWebImage

The images are getting loaded every time a cell is loaded, because the imageWithData: doesn't use any cache. 每次加载单元格时都会加载图像,因为imageWithData:不使用任何缓存。

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 如果是答案,我会投票赞成

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 ie when the image load complete the image will show but the table will be loaded when the table view is needed to load. 这将进行异步调用,并在加载tableview后加载图像,即,当图像加载完成时,将显示图像,但在需要加载表视图时将加载表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM