在uicollectionview中延迟加载图像问题

Nam*_*Sen 3 ios uicollectionview

我是iOS应用开发的新手.我正在处理延迟加载的图像UICollectionView.图像正在显示,但是当我滚动UICollectionView图像时,图像会改变它们的位置,并且重复图像,其中所有图像应该是唯一的.这是我的代码:

- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];

    cell.imageview_collectnvw.layer.cornerRadius=6.0f;
    cell.imageview_collectnvw.layer.masksToBounds=YES;

    dispatch_queue_t queue = dispatch_queue_create("patientlist",NULL);
    dispatch_async(queue, ^{


        NSString *str=[[[search_array objectAtIndex:indexPath.row]valueForKey:@"friend"]valueForKey:@"linkedin_photo"];

        NSLog(@"cell index path --- is %d",indexPath.row);

        NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
        dispatch_sync(dispatch_get_main_queue(), ^{

            if ([[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {

                MyCell *cell = (MyCell*)[collectionView cellForItemAtIndexPath:indexPath];

                cell.imageview_collectnvw.image=[UIImage imageWithData:data];

            }

            [cell setNeedsLayout];

            NSLog(@"cell index path main is %d",indexPath.row);

        });
    });


    return cell;
}
Run Code Online (Sandbox Code Playgroud)

这可能是一个愚蠢的问题,但我需要答案..如果有人能够回答这个问题,将会有很大的帮助.提前致谢.

kon*_*ier 5

UICollectionView您合作时应使用indexPath.item而不是indexPath.row.