Xcode表格视图在滚动时滞后

(void)bilgileriYukle { NSMutableArray *yemekIsimleri = [[NSMutableArray alloc] init]; NSMutableArray *resimIsimleri = [[NSMutableArray alloc] init]; NSURL *myURL = [[NSURL alloc]initWithString:@"http://www.sevgilezzeti.com/WebServices/tarifler.php"]; NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL]; if (myData == nil) { NSLog(@"INTERNET YOK YADA VERI CEKEMEDI"); } else { NSLog(@"INTERNET VAR VERI CEKILDI"); NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:kNilOptions error:nil]; for (id element in jsonArray) { NSString *durum = [element objectForKey:@"durum"]; if([durum isEqualToString:@"1"]) { [yemekIsimleri addObject:[element objectForKey:@"yemekadi"]]; NSString *gelenYemekAdi =[element objectForKey:@"kapakresmi"]; NSString *imagePathKapak = @"http://sevgilezzeti.com/Admin/yemekresimkapak/"; combined = [NSString stringWithFormat:@"%@%@", imagePathKapak, gelenYemekAdi]; [resimIsimleri addObject:combined]; } } } _TarifAdi=yemekIsimleri; _ResimAdi=resimIsimleri; } 

////////////////////////////////////////////////// /////////////////////////////////////

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath]; NSUInteger row = [indexPath row]; cell.TitleLabel.text = _TarifAdi[row]; NSCache *_imageCache; UIImage *image = [_imageCache objectForKey:@"DenemeRes"]; if (image == nil) { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_ResimAdi[row]]]; image = [UIImage imageWithData:data]; [_imageCache setObject:image forKey:@"DenemeRes"]; } cell.ThumbImage.image = image; return cell; } 

我可以从URL获取图像,并可以看到图像,但是当我滚动表格视图时,它非常缓慢和滞后。 我该如何解决这个问题?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath]; cell.tag = indexPath.row; cell.imageView.image = nil; // Rounded Rect for cell image CALayer *cellImageLayer = cell.imageView.layer; [cellImageLayer setCornerRadius:35]; [cellImageLayer setMasksToBounds:YES]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async(queue, ^(void) { NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_ResimAdi[indexPath.row]]]; UIImage *image = [[UIImage alloc] initWithData:data]; if (image) { dispatch_async(dispatch_get_main_queue(), ^{ if (cell.tag == indexPath.row) { CGSize itemSize = CGSizeMake(70, 70); UIGraphicsBeginImageContext(itemSize); CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height); [image drawInRect:imageRect]; cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [cell setNeedsLayout]; } }); } }); cell.TitleLabel.text = _TarifAdi[indexPath.row]; return cell; } 

在表格视图单元格中同步加载图像是非常简单和直接的,但是这是很糟糕的,因为它在滚动时会导致滞后,因为它实际上是在滚动时加载和显示图像。

因此,必须避免同步加载图像,而是使用除主线程以外的其他线程来asynchronous加载图像,以便滚动和加载+显示图像可以独立完成,避免任何forms的延迟。

这可能是一个重复的问题,因为它已经被问及有教程,但由于我自己总是有一个问题,在我的第一个iOS应用程序,发现它很混乱,我在这里发布的答案,希望是很有帮助。

尝试添加这样的东西到你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 // Get the filename to load. NSString *imageFilename = [imageArray objectAtIndex:[indexPath row]]; NSString *imagePath = [imageFolder stringByAppendingPathComponent:imageFilename]; [[cell textLabel] setText:imageFilename]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; dispatch_sync(dispatch_get_main_queue(), ^{ [[cell imageView] setImage:image]; [cell setNeedsLayout]; }); }); 

dataWithContentsOfURL阻止你的主线程,这就是为什么表是这样做的。 您应该在后面的线程中加载图像,以便主线程可以不中断地处理UI

 NSURL *url = [NSURL URLWithString:link]; NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:0 timeoutInterval:kTimeoutInterval]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { UIImage *image = [[UIImage alloc] initWithData:self.activeDownload]; // set to ImageView cell.ThumbImage.image = image; }];