我刚接触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;
}
这可能是一个愚蠢的问题,但我需要答案...如果有人能回答这个问题,那将是非常有帮助的。提前致谢。
indexPath
的section
属性。 - konrad.bajtyngier