如何解决一个慢滚动表视图

我有一个缓慢滚动的表格视图。 有谁知道这可能是为什么?

每一行都有一个图像,但是即使在图像加载之后,它仍然会口吃和缓慢滚动。

谢谢你的帮助

这是我的代码:

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

这是由于您使用的图像加载机制。

您正在主线程中从url加载图像。 这就是为什么用户界面被阻止了一段时间dataWithContentsOfURL:是一个同步调用。 所以用户界面将获得图像数据后作出回应。

苹果表示,像webrequest,parsing大量数据等进程的时间必须在其他线程而不是主线程上完成。 所有与UI相关的任务都必须在主线程上完成。

解决scheme:

  1. 在后台线程中请求图像,而不是在主线程中。
  2. caching图像,一旦你得到它

源代码和第三方库

这里有一些链接可以帮助您理解使用asynchronous方法装载图像的基本思路

  1. LazyTableImages
  2. HJCache
  3. SDWebImage

每次加载单元格时都会加载图像,因为imageWithData:不使用任何caching。

编辑:我看到一个意见,build议加载图像asynchronous。 你已经有了每个单元的自定义类,所以它应该很容易做到。 如果这是一个答案,我会投它

我想你是想说这个。

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

这将使asynchronous调用,加载tableview加载后的图像,即当图像加载完成后,图像将显示,但表中将加载时,需要加载表视图。