0

こんにちは私はGCDを使用してuitableviewに画像をロードしています。さて、問題は画像アニメーションです。セルに読み込まれるサーバーから3つの画像を呼び出しましたが、3つの問題が発生しました

1)3つのセルすべてで単一の画像が繰り返されます

2)最初と最後の細胞画像が点滅して変化する

3)画像が変化すると、すべてのセルで繰り返されます。

私はアニメーションを与えましたが、それでもアニメーションはありません。つまり、点滅します。

コード:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...

[imageArray retain];

//get a dispatch queue
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
 //this will start the image loading in bg
 dispatch_async(concurrentQueue, ^{ 
     NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[imageArray objectAtIndex:indexPath.row]]];

     //this will set the image when loading is finished
     dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = [UIImage imageWithData:image];

         [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
         });
    });

return cell;

}

4

1 に答える 1

1

この場所で画像をダウンロードしないでください。

行が多すぎない場合は、たとえば、これを実行します。viewDidLoadを実行し、ダウンロードした画像を新しい可変配列に保存します。

このようなもの:

- (void) viewDidLoad {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
        int row = 0;
        self.thumbnails = [NSMutableArray array];
        for (NSString *imageURLString in imageArray) {
        //Your way of download images
        NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLString]];
        UIImage *image = [UIImage imageWithData:imageData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.thumbnails addObject:image];
            NSArray *indexes = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:1]];
            [self.tableViewOutlet reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone];
        });
        row++;
    }
});
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (self.thumbnails && [self.thumbnails count] > indexPath.row) {
            cell.attachmentImageView.image = [self.thumbnails objectAtIndex:indexPath.row];
        } else {
            cell.attachmentImageView.image = nil;
        }
}
于 2012-07-25T08:04:04.860 に答える