我不認爲你需要加載更多的細胞,因爲這是自然的行爲。如果您的滾動不順暢,也許是因爲您沒有正確加載單元格中的圖像。
您必須申請lazy loading pattern。例如,你可以這樣做(假設你已經設置了NSMutableDictionary* imageDic = [[NSMutableDictionary alloc] init];
,你的細胞擁有財產@property(nonatomic, retain) UIImageView *imageView;
,並且您使用AFNetworking)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView_ cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView_ dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
NSString* cellKey = [NSString stringWithFormat:@"%d_%d", indexPath.section, indexPath.row];
NSString* imgName = [cellKey stringByAppendingPathExtension:@"jpg"];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
BOOL isDirectory = NO;
NSFileManager* fm = [NSFileManager defaultManager];
// set image with image in cache (it will be fast!)
if ([imageDic objectForKey:cellKey])
{
UIImage *img = [imageDic objectForKey:cellKey];
cell.imageView.image = img;
}
// set image with image saved in document directory and put it in cache
else if ([fm fileExistsAtPath:imagePath isDirectory:&isDirectory])
{
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
cell.imageView.image = img;
[imageDic setObject:img forKey:cellKey];
}
// download image at imageURL, save it and put it in cache too. Until then set image with a placeholder
else
{
__weak UICollectionViewCell* weakCell = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:imageURL] placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
[imageDic setObject:img forKey:cellKey];
[UIImageJPEGRepresentation(img, 1.f) writeToFile:imagePath atomically:YES];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"failed to dl image");
}];
}
return cell;
}