【问题标题】:UITableView content of a row loads after scrolling the cell out of the view将单元格滚动出视图后加载一行的 UITableView 内容
【发布时间】:2014-09-18 19:13:01
【问题描述】:

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

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

这是我的 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;
}

【问题讨论】:

    标签: ios objective-c iphone xcode uitableview


    【解决方案1】:

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

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

    编辑:

    由于您已经使用主线程重新加载表,我怀疑图像下载需要时间,进而阻塞线程。我还观察到您的视频中发生了一些故障。尝试使用异步方法下载图像并检查。 initWithContentsOfURL 会阻塞你的线程,在这个地方不是一个好的选择。

    【讨论】:

    • 嗯,我将它添加到我的代码中,但没有任何机会。还是同样的问题:/
    • 编辑了我的答案。检查一下。
    【解决方案2】:

    试试这个代码。如有任何疑问,请告诉我...

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

    【讨论】:

    • 另外,您需要一些机制来避免每次调用 cellForRowAtIndexPath 方法时都加载图像数据。首先,您尝试上面的代码。那我们就往前走。
    • 好的......首先感谢@Pratik,但我仍然有同样的问题......但现在我知道,他正确下载了所有图像,但他释放单元格为时已晚。内容已下载,但滚动后单元格将释放。那它为什么要这样做呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多