2014-09-18 82 views
1

问题是,在将单元格滚动出视图后,单元格的内容加载: 我不知道如何正确描述它,所以我记录短10秒视频:http://youtu.be/GJouA3IXY0M在将单元格滚动出视图后加载一行的UITableView内容

,我不知道如何解决它......

这是我的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    PlaylistenTableViewCell *cell = [self.playlistenTableView dequeueReusableCellWithIdentifier:@"Cell"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    //titleLabel = label which shows the big headline 
    cell.titleLabel.text = [self.JsonTitle objectAtIndex:indexPath.row]; 
    cell.titleLabel.layer.cornerRadius = 5; 
    cell.titleLabel.layer.masksToBounds = YES; 


    NSString *urlString = [self.JsonContent objectAtIndex:indexPath.row]; 
    NSURL *url = [NSURL URLWithString:urlString]; 

    NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
    UIImage *tmpImage = [[UIImage alloc] initWithData:data]; 


    //playlistThumbnailImageView = the imageVIew which shows the Image 
    cell.playlistThumbnailImageView.image = tmpImage; 
    [cell.playlistThumbnailImageView setOpaque:NO]; 
    [cell.playlistThumbnailImageView setBackgroundColor:color]; 
    cell.playlistThumbnailImageView.layer.cornerRadius = 5; 
    cell.playlistThumbnailImageView.layer.masksToBounds = YES; 
    cell.playlistThumbnailImageView.layer.borderWidth = 4.0; 


    return cell; 
} 

回答

1

很可能你重装与后台线程表。用主线程进行UI更新。因此,在您的数据在后台获取后,请使用此代码。

dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.myTableView reloadData]; 
    }); 

编辑:

既然你已经使用主线程已经重装了表,我怀疑图像花时间下载并反过来被阻塞线程。我还观察到您的视频中出现了一些故障。尝试使用异步方法下载图像并检查。 initWithContentsOfURL会阻止你的线程,在这个地方不是一个好的选择。

+0

嗯,我将此添加到我的代码中,但没有任何机会。仍然是同样的问题:/ – user3708025 2014-09-18 19:25:38

+0

编辑我的答案。请检查。 – user1190882 2014-09-18 19:35:02

0

试试看看这个代码。如果有任何查询,请告知我...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ------- 
    ------- 

    NSString *imageUrl = @"-- URL string --"; 

    NSURL *urlImage = [NSURL URLWithString:imageUrl]; 
    [imgVw setImage:[UIImage imageNamed:@"placeholder.png"]]; 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 
    dispatch_async(queue, ^{ 
     NSError* error = nil; 
     NSData *imageData = [NSData dataWithContentsOfURL:urlImage options:nil error:&error]; 

     if(error){ 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       [imgVw setImage:[UIImage imageNamed:@"placeholder.png"]]; 
       [self.loadingIndicator stopAnimating]; 
       [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
      }); 
     }else{ 
      UIImage *image = [UIImage imageWithData:imageData]; 

      dispatch_sync(dispatch_get_main_queue(), ^{ 
       [self.imgVw setImage:image]; 
       [self.loadingIndicator stopAnimating]; 
       [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
      }); 
     } 
    }); 
} 
+0

另外,您需要一些机制来避免每次_cellForRowAtIndexPath_方法调用时加载图像数据。首先,你尝试上面的代码。然后我们会前进。 – NSPratik 2014-09-19 12:24:53

+0

好的......首先感谢@Pratik,但我仍然有同样的问题......但现在我知道,他正确地下载了所有图像,但他太晚释放了这个单元格。内容已经下载了,但是在我滚动它之后,单元格会被释放。那为什么要这样做呢? – user3708025 2014-09-27 14:13:08

相关问题