实现NSURL的UITableView上的GCD

时间:2013-08-21 17:00:56

标签: ios uitableview grand-central-dispatch

我一直试图在我的项目上实现一个GCD,它显示了从XML获取数据,即使它是我第一次这样做,我已经成功地(也许)在nameLabel和detailLabel上实现了这两个都是字符串,但是imageLabel(代码的注释部分)在我尝试实现与两个字符串相同的GCD时没有给出任何内容,我不知道发生了什么但是当我运行项目时它给出了一个未知的异常,我想知道如何如何在代码的注释部分实现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;

}

1 个答案:

答案 0 :(得分:3)

您需要将更新单元格的部分发送回主线程,就像前两个部分一样。这部分: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进行其他操作。)但实际上,只需使用异步图像下载管理器。