如何停止NSInvocationOperation和NSOperationQueue

时间:2012-10-11 06:31:23

标签: iphone objective-c xcode uitableview

我在表格视图中使用[NSOperationQueue mainQueue]和NSInvocationOperation,以在后台加载来自web的图像。但是在下载图像后它没有停止。这是我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=[mytable dequeueReusableCellWithIdentifier:@"cell"];


    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }

    imageLabel=(UIImageView *)[cell viewWithTag:5];
    UIImage *image=[dict objectForKey:[appSmallLogos objectAtIndex:indexPath.row]];
    if (image) {
        imageLabel.image=image;
    } else {   
        NSURL *on=[appSmallLogos objectAtIndex:indexPath.row];
        NSString *on1=[appNames objectAtIndex:indexPath.row];


        NSMutableArray *array=[[NSMutableArray alloc] initWithObjects:on,on1,indexPath, nil];
        operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(didGetAlbums:) object:arr];

        q =[NSOperationQueue mainQueue];
        [q addOperation:operation];

    imageLabel.image=[dict objectForKey:on1];
    }

    return cell;
}

//didgetalbums method

- (void )didGetAlbums: (NSMutableArray* )url
{

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    dispatch_async(queue, ^{
        NSData *data=[NSData dataWithContentsOfURL:[url objectAtIndex:0]];
        UIImage *image1 = [UIImage imageWithData:data];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [dict setObject:image1 forKey:[url objectAtIndex:1]];

        });
    });

    //reload the requested cell after download image
    [self. mytable beginUpdates];
    [self. mytable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[url objectAtIndex:2], nil] withRowAnimation:UITableViewRowAnimationNone];
    [self. mytable endUpdates];    // imageLabel.image=[dict objectForKey:[appSmallLogos objectAtIndex:indexPath.row]];
}

0 个答案:

没有答案