1

我正在尝试保存我cellForRowInIndexPathUITableViewController.

不幸的是,该数组未完全填充或顺序不正确。

我的tableview:cellForRowAtIndexPath方法中的代码是:

// populate the cell's image
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

dispatch_async(queue, ^{
    NSURL *url = [NSURL URLWithString:[newsModel url]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    img = [[UIImage  alloc] initWithData:data] ;
    [imageArray insertObject:img atIndex:[indexPath row]];

    dispatch_sync(dispatch_get_main_queue(), ^{
        // make image standard size
        CGSize imageSize = CGSizeMake(40, 40);
        UIGraphicsBeginImageContext(imageSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
        [img drawInRect:imageRect];

        cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        [cell setNeedsLayout];
    });
});

imageArray( NSMutableArray) 和 img( UIImage) 都是类型__bool

在此先感谢您的帮助。

4

2 回答 2

2

AllValue尝试使用键代替数组存储到字典中,因为在使用字典插入数组后没有机会重复

例如:

imageArray =[[dict allValues]mutableCopy];
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:[newsModel url]];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage  alloc] initWithData:data] ;

[dict setObject:img forKey:indexPath.row]
dispatch_sync(dispatch_get_main_queue(), ^{
// make image standard size
CGSize imageSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(imageSize);
CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);
[img drawInRect:imageRect];

cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[cell setNeedsLayout];
});
});
imageArray =[[dict allValues]mutableCopy];
于 2013-05-07T12:11:09.760 回答
0

下载完成后不要尝试修改单元格,你不知道它是否被重复使用。只需保存图像,然后要求表格视图reloadData(确保您要求在主线程上重新加载)。

于 2013-05-07T12:00:06.403 回答