2016-03-21 11 views
0
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
cell =[self.imgCllvw dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
     if(!cell) 
     { 
      cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
     } 
     NSDictionary *tmpDict = [images objectAtIndex:indexPath.row]; 

     NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:@"img"]]; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *imge= [[UIImage alloc]initWithData:data]; 
       dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.imageView.image = imge; 
       }); 
     }); 
     cell.layer.borderWidth=1.0f; 
     cell.layer.borderColor=[UIColor colorWithRed:215.0/255.0 green:214.0/255.0 blue:214.0/255.0 alpha:1.0].CGColor; 

       return cell; 
} 

collectionviewセルに表示されていないと私はcollectionviewをスクロールする場合は画像が表示され、私はあなたがの非同期ダウンロードを試みるべきだと思いcollectionview最初のイメージは最初のイメージがロードされていない

+0

もう1つのことは、 'UICollectionView'デリゲートとdataSourceメソッドで' row'を参照しないことです。 'indexPath.item'を参照してください。 – n00bProgrammer

答えて

0

のために自動レイアウトを使用していましたむしろ同期より画像、代わりに、あなたはこのように、dispatch_syncを使用することができますdispatch_async、mainqueueのこの

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    cell =[self.imgCllvw dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    if(!cell) 
    { 
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    } 
    NSDictionary *tmpDict = [images objectAtIndex:indexPath.row]; 

    NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:@"img"]]; 
    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    if (data) { 
     UIImage *image = [UIImage imageWithData:data]; 
     if (image) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      MyCell *cell = (id)[collectionView cellForItemAtIndexPath:indexPath]; 
      if (cell) 
      cell.imageView.image = image; 
     }); 
     } 
    } 
    }]; 
    [task resume]; 
    cell.layer.borderWidth=1.0f; 
    cell.layer.borderColor=[UIColor colorWithRed:215.0/255.0 green:214.0/255.0 blue:214.0/255.0 alpha:1.0].CGColor; 

    return cell; 
} 
+0

しかし、まだ画像をロードしてシャッフルしている場合は、 –

+0

コレクションセルが再利用されるようになります。ダウンロードした画像データを保存していますか?配列/コアデータに保存せずに画像データが存在する場合は、ダウンロード前に確認してください –

0

のようなもの、

dispatch_sync(dispatch_get_main_queue(), ^{ 
    cell.imageView.image = imge; 
      }); 
関連する問題