我是 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;
}
这可能是一个愚蠢的问题,但我需要答案。如果有人能回答这个问题,那将有很大帮助。提前致谢。