3

我使用控制台检查它是否重新加载并且它在cellForRowAtIndexPath. 它会导致表格视图卡住片刻。我以为是因为图像,但现在我无法弄清楚问题所在。任何帮助都会很棒。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog( @"Cell loading" );
    static NSString *CellIdentifier = @"showVideo";
    CustomVideoCell *cell = (CustomVideoCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell=[[CustomVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }


    NSURL *url = [NSURL URLWithString:[data objectForKey:@"profilepicture"]];
    NSData *data = [NSData dataWithContentsOfURL:url];

    [cell.playimage setImageWithURL:[NSURL URLWithString:[data objectForKey:@"profilepicture"]]
                 placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;

}
4

2 回答 2

3

这一行提出了问题:

NSData *data = [NSData dataWithContentsOfURL:url];

这是一个同步调用,这就是为什么您在滚动时会感到延迟。

切勿在主线程上调用此类方法,这将导致您的应用程序无响应。如果数据太高,则下载需要更多时间,从而需要更多时间来响应 UIEvents。

You need to cache the images once it's downloaded, else each time when the table is reloaded it'll download it again. If the image is already downloaded you need to display it from the cache don't download it again. You need to handle this on your cellForRowAtIndexPath. You can write your own code for doing these tasks or you can rely on the following samples.

Check the following libraries/source code:

  1. HJCache
  2. LazyTableImages
  3. SDWebImage
于 2012-12-19T09:19:18.050 回答
0

Use SDWebImage. It makes it much easier to download images asynchronously and manages the caching for you.

于 2012-12-19T09:22:40.457 回答