0

GCDで画像をダウンロードする機能を備えたImageDownloaderクラスがあります:

- (void) downloadImageFromURL:(NSURL *) url completionBlock:(void (^)(UIImage *image, NSError *error)) block {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    UIImage *picture=[UIImage imageWithData:imageData];
    if(picture) {
        block(picture, nil);
    } 
    else {
        NSError *error = [NSError errorWithDomain:@"image_download_error" code:1 
                                         userInfo:[NSDictionary dictionaryWithObject:@"Can't fetch data" forKey:NSLocalizedDescriptionKey]];
        block(nil, error);
    }

  });
}

私のtableviewcell(独自のnibファイルで定義)は、次のようにすべての行に対してこの関数を呼び出します:

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

//custom cell in nib file
static NSString *MyIdentifier = @"contactsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"contactsCell" owner:self options:nil]; //feedCell je ime nib fajla
    cell = contactsCell;
    self.contactsCell = nil;
}  

if (indexPath.section == 0) {
    UIImageView *avatar=(UIImageView *) [cell viewWithTag:1];
    avatar.image=[UIImage imageNamed:@"petek.jpg"];
    UILabel *nameLabel= (UILabel *) [cell viewWithTag:2];
    nameLabel.text=@"Some guy";
    UIButton *askBtn=(UIButton *) [cell viewWithTag:3];
    [askBtn setTitle:@"Ask" forState:UIControlStateNormal];
    [askBtn addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];

}
else {
    UIImageView *avatar=(UIImageView *) [cell viewWithTag:1];
    ImageDownloader *idl=[[ImageDownloader alloc] init];
    NSURL *imageUrl=[NSURL URLWithString:@"http://someImageUrl"];
    [idl downloadImageFromURL:imageUrl completionBlock:^(UIImage *image, NSError *error)
     {
         if(!error) {
             dispatch_sync(dispatch_get_main_queue(), ^(void) {
                 avatar.image=image;
             });
         } else {
             NSLog(@"error %@", error);
         }

     }];
    UILabel *nameLabel= (UILabel *) [cell viewWithTag:2];
    nameLabel.text=@"John Stockton";
    UIButton *followBtn=(UIButton *) [cell viewWithTag:3];
    [followBtn setTitle:@"Follow" forState:UIControlStateNormal];
    [followBtn addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];

}

    return cell;    

}

セクション 1 の画像は正しく読み込まれますが、スクロールすると、セクション 1 の行にセクション 0 の画像が一瞬だけ表示されることがあります。

私は何を間違っていますか?

4

3 に答える 3

1

という名前の行の一意の識別子を再利用しているためMyIdentifier、古い行のイメージをキューから取り出してからデータを再ロードしている可能性があります。

次のような静的識別子の場合は、代わりにこれを使用できます。

NSString *MyIdentifier = [NSString stringWithFormat:@"contactsCell-%d-%d",indexPath.section,indexPath.row];
于 2012-07-03T12:51:20.187 に答える
1

ダウンロードを開始する前に行うavatar.image = nilと、この問題は発生しません。これは、セルが画面に表示されてから、セルの画像のダウンロードが完了するまでに時間差があるために発生しています。

于 2012-07-03T12:47:33.280 に答える
0

UIImageView *avatar=(UIImageView *) [cell viewWithTag:1]; セル内に情報を運ぶことはありません。それらは再利用可能です。

于 2012-07-03T12:52:40.373 に答える