如何在“无滚动”的UICollectionView中加载图像?

时间:2015-12-29 07:00:55

标签: ios objective-c uicollectionview lazy-loading uicollectionviewcell

我可以使用以下代码在UICollectionView中加载图像,但问题是,它会在屏幕滚动后加载图像。如果没有Scroll,它将只显示空单元格
注意: - 我没有从任何链接(API)加载图像,它来自images.xcassets文件或支持文件。

以下是UICollectionView的代码:

#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
collectionImages = [NSArray arrayWithObjects:@"car1.jpeg",@"car2.jpeg",@"car3.jpeg",@"car4.jpeg",@"car5.jpeg",@"car6.jpeg", nil];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return collectionImages.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier =@"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImageView * collectionImageView = (UIImageView *) [cell viewWithTag:100];

    collectionImageView.image = [UIImage imageNamed:[collectionImages objectAtIndex:indexPath.item]];
return cell;
}

@end


下面的图像,当我运行程序但没有滚动。如果我滚动,它将逐个加载图像 enter image description here

1 个答案:

答案 0 :(得分:0)

致电

[collectionImageView reloadData]

at

- (void)viewWillAppear:(BOOL)animated