ios - NSURL 实现的 UITableView 上的 GCD

标签 ios uitableview grand-central-dispatch

我一直在尝试在我的项目上实现一个 GCD,它显示从 XML 中获取数据,尽管这是我第一次这样做我已经成功(可能)在 nameLabel 和 detailLabel 上实现,它们都是字符串,但是当我尝试实现与两个字符串相同的 GCD 时,imageLabel(代码的注释部分)没有提供任何信息,我不知道发生了什么,但是当我运行该项目时,它给出了一个未知的异常,我想知道如何在代码的注释部分实现 GCD,这样我就可以在 imageView 中显示图像。

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


    static NSString *CellIdentifier = @"CustomCell";

    dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
    CustomCellXMLClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    if(cell == nil) {
        cell = [[CustomCellXMLClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellXMLSample" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(myQueue, ^{
        NSString *nameLabel = [currentData nameOfCat];
        NSString *dataToCacheLabel = [myCache objectForKey:nameLabel];
        if(nameLabel != nil){
            dataToCacheLabel = [NSString stringWithString:nameLabel];
                if (dataToCacheLabel != nil) {
                    [myCache setObject:dataToCacheLabel forKey:nameLabel];
                    dispatch_async(dispatch_get_main_queue(), ^{
                    [cell.nameLabel setText:dataToCacheLabel];
                 });
                }
        }



        NSString *detailLabel = [currentData descriptionOfCat];
        NSString *stringToCache = [myCache objectForKey:detailLabel];
        if (detailLabel != nil) {
            stringToCache = [NSString stringWithString:detailLabel];
                if (stringToCache != nil) {
                    [myCache setObject:stringToCache forKey:detailLabel];
                    dispatch_async(dispatch_get_main_queue(), ^{

                    [cell.detailLabel setText:stringToCache];
                   });
                }
        }

//        NSString *imageURL = [currentData imageLink];
//        NSData *dataToCache;
//            if (imageURL != nil) {
//                
//                dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
//                if (dataToCache != nil) {
//                    
//                    [myCache setObject:dataToCache forKey:imageURL];
//                    [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
//                    
//                }
//                else {
//                    
//                    NSURL *imageURL = [NSURL URLWithString:@"http://i178.photobucket.com/albums/w255/ace003_album/190579604m.jpg"];
//                    dataToCache = [NSData dataWithContentsOfURL:imageURL];
//                    [myCache setObject:dataToCache forKey:imageURL];
//                    [cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
//                }
//            
//            }
        [self.activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];

    });
         return cell;

}

最佳答案

您需要将更新单元格的部分分派(dispatch)回主线程,就像您对前两个部分所做的那样。这部分:dispatch_async(dispatch_get_main_queue(),...) 它可能看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    static NSInteger usageCount = 0;

    dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
    CustomCellXMLClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {
        cell = [[CustomCellXMLClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellXMLSample" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Give it a new tag every time, so we can tell if this is "our" use of the cell
    cell.tag = ++usageCount;

    myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(myQueue, ^{
        NSString *nameLabel = [currentData nameOfCat];
        NSString *dataToCacheLabel = [myCache objectForKey:nameLabel];
        if(nameLabel != nil){
            dataToCacheLabel = [NSString stringWithString:nameLabel];
            if (dataToCacheLabel != nil) {
                [myCache setObject:dataToCacheLabel forKey:nameLabel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (cell.superview && usageCount == cell.tag)
                    {
                        [cell.nameLabel setText:dataToCacheLabel];
                    }
                });
            }
        }

        NSString *detailLabel = [currentData descriptionOfCat];
        NSString *stringToCache = [myCache objectForKey:detailLabel];
        if (detailLabel != nil) {
            stringToCache = [NSString stringWithString:detailLabel];
            if (stringToCache != nil) {
                [myCache setObject:stringToCache forKey:detailLabel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (cell.superview && usageCount == cell.tag)
                    {
                        [cell.detailLabel setText:stringToCache];
                    }
                });
            }
        }

        NSURL *fallbackImageURL = [NSURL URLWithString:@"http://i178.photobucket.com/albums/w255/ace003_album/190579604m.jpg"];
        NSString *imageURL = [currentData imageLink];
        NSArray* urls = imageURL ? @[ imageURL, fallbackImageURL ] : @[ fallbackImageURL ];
        for (NSURL* url in urls)
        {
            UIImage* cachedImage = [myCache objectForKey: url];
            if (!cachedImage)
            {
                NSData* imageData = [NSData dataWithContentsOfURL:url];
                cachedImage = [UIImage imageWithData:dataToCache];
                [myCache setObject:cachedImage forKey:url];
            }

            if (cachedImage)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (cell.superview && usageCount == cell.tag)
                    {
                        [cell.imageShow setImage: uiImage];
                    }
                });
                break;
            }
        }
    });
    return cell;
}

从长远来看,您可能还应该考虑使用多种异步加载方法中的任何一种来代替 dataWithContentsOfURL:,后者会阻塞后台线程等待数据被完全接收。但这是次要问题,而不是你问的问题。

编辑: 针对@Rob 的评论进行了编辑。在获取之前检查缓存中是否有预缓存的图像,并防止将值写入重用或不可见的单元格(假设您没有将 tag 用于其他用途。)但实际上,只需使用异步图像下载管理器。

关于ios - NSURL 实现的 UITableView 上的 GCD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363501/

相关文章:

iOS Rxswift 处理 CancelButton 在 searchBar 中点击

iphone - iOS:在自动布局下更改 UITableView 高度

ios - 只有单个 uitableview 单元格可以拖动和重新排序

iphone - 在 TableView 中使用 GCD 和加载缩略图时遇到问题

ios - 在 iOS block 和队列中运行多个任务的最佳实践是什么?

ios - 使用形状蒙版 Swift 隐藏文本

ios - 如何将 iPhone 应用程序常量(.h 类)和第三方框架添加到 watch 套件扩展中?

ios - Carthage: 没有适用于 iOS 平台的共享框架方案(针对我自己的框架)

ios - 如何将 PFQueryViewController 用作 UITableViewController 的 subview ?

memory-management - 每个自定义 GCD 队列都需要 ARC 下的自动释放池吗?