1

で画像を非同期にロードしようとしていUITableVIewます。

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

UITableViewCell *cell = nil;
NSString *cellIndetify = [NSString stringWithFormat:@"cell%d",tableView.tag -TABLEVIEWTAG];

cell = [tableView dequeueReusableCellWithIdentifier:cellIndetify];
IndexPath *_indextPath = [IndexPath initWithRow:indexPath.row  withColumn:tableView.tag - TABLEVIEWTAG];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetify];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor blackColor];
// here I init cell image view
        UIView *cellSub =  [self.waterFlowViewDatasource waterFlowView:self cellForRowAtIndexPath:_indextPath];

    cellSub.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:cellSub];
    cellSub.tag = CELLSUBVIEWTAG;

}
// fill image info 
    [self.waterFlowViewDatasource waterFlowView:self fillDataIntoCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath];


CGFloat cellHeight = [self.waterFlowViewDelegate waterFlowView:self heightForRowAtIndexPath:_indextPath];

CGRect cellRect = CGRectMake(0, 0, _cellWidth, cellHeight);
[[cell viewWithTag:CELLSUBVIEWTAG] setFrame:cellRect];


[self.waterFlowViewDatasource waterFlowView:self relayoutCellSubview:[cell viewWithTag:CELLSUBVIEWTAG] withIndexPath:_indextPath];

return cell;
}

そのImageViewクラスで私は初期化します:

(id)initWithIdentifier:(NSString *)indentifier
{
    if(self = [super initWithIdentifier:indentifier])
    {
        self.backgroundColor = [UIColor blackColor];

        if (!self.imageView) {
            _imageView = [[UIImageView alloc] init];
            self.imageView.backgroundColor = IMAGEVIEWBG;
            [self addSubview:self.imageView];

            self.imageView.layer.borderWidth = 1;
            self.imageView.layer.borderColor = [[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0] CGColor];
        }
        ......some others controllers
}

-(void)setImageWithURL:(NSURL *)imageUrl{


    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromKey:[imageUrl absoluteString]];
    if (cachedImage) {
        self.imageView.image = cachedImage;
    }else{
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
            [manager downloadWithURL:imageUrl delegate:self options:SDWebImageCacheMemoryOnly userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[imageUrl absoluteString], @"image_url", [NSValue valueWithBytes:&imageSize objCType:@encode(CGSize)], @"image_size", [NSNumber numberWithInt:arrIndex], @"imageview_index", nil]];
            _imageView.image = [UIImage imageNamed:@"album_detail_placeholder.png"];
}
.....
.....

- (void)imageDecoder:(SDWebImageDecoder *)decoder didFinishDecodingImage:(UIImage *)image userInfo:(NSDictionary *)userInfo {

       imageView.image = image
}

ご覧のとおり、私は SDWebImageManager を使用しましたが、それは問題ではないようです。

次のページにプルアップすると、次のページの画像が読み込まれる前に、前のページの読み込まれた画像にスクロールして戻ると、画像が別の画像で覆われます (現在のページではなく、最新のページに表示されるはずです...) (たとえば、最初のページに画像があり、この画像に猫がいます。次に、次のページを要求するためにプルアップするまで下にスクロールし、次のページの画像の URL をすべて取得した後、画像の非同期スレッド ダウンロードを開始します。ダウンロードされた最新の画像, スクロールして最初のページ. ) 何をすればよいでしょうか?

4

1 に答える 1

1

行が画面外にスクロールされると、テーブル ビューはその行のセルを画面上にスクロールされた別の行に再利用します。

問題は、 をImageViewバックグラウンド ローダーのデリゲートとして使用していてImageView、 が行ではなくセルに関連付けられていることです。バックグラウンド ローダーが画像の読み込みを完了するまでに、セルが別の行に再利用されている可能性があります。

テーブル ビューのデータ ソースを背景画像ローダーのデリゲートにする必要があります。userInfoを呼び出すときに、行のインデックス パスをディクショナリに入れる必要がありますdownloadWithURL:delegate:options:userInfo:。ダウンローダがデータ ソースに送信imageDecoder:didFinishDecodingImage:userInfo:するとき、データ ソースは からインデックス パスを取得し、userInfoそれを使用して、現在その行を表示しているセルをテーブル ビューに問い合わせます (cellForRowAtIndexPath:テーブル ビューに送信することにより)。

于 2012-12-31T17:53:41.763 に答える