无论如何,在显示单元格时侵入 UICollectionView 以调用 willDisplayCell 委托方法?
我需要这个来延迟加载,我用 UITableView 做得很好,但 UICollectionView 官方没有那种委托方法。
那么,有什么想法吗?谢谢!
无论如何,在显示单元格时侵入 UICollectionView 以调用 willDisplayCell 委托方法?
我需要这个来延迟加载,我用 UITableView 做得很好,但 UICollectionView 官方没有那种委托方法。
那么,有什么想法吗?谢谢!
仅供参考,这已添加到 iOS 8 的 API 中:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
我正在做这样的事情cellForItemAtIndexPath:
(用于延迟加载图像):
cellForItemAtIndexPath
原始 indexPath。由于图像现在存在于模型中,因此现在将设置单元格的图像。看起来像这样:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
id imageItem = [self.photoSet objectAtIndex:indexPath.row];
ImageManager * listingImage = (ImageManager *)imageItem;
if(listingImage.image){
cell.image = listingImage.image;
} else {
[listingImage loadImage:^(UIImage *image) {
[collectionView reloadItemsAtIndexPaths:@[indexPath]];
dispatch_async(dispatch_get_main_queue(), ^{
if ([[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {
[(PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath] setImage:image];
}
});
}];
}
}
return cell;
}
您可以使用SDWebImage
https://github.com/rs/SDWebImage进行延迟加载。你可以有效地使用它UICollectionView
。
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
/*---------
----Other CollectiveView stuffs------
-----------------*/
if([[NSFileManager defaultManager] fileExistsAtPath:YOUR_FILE_PATH isDirectory:NO])
{
imagView.image=[UIImage imageWithContentsOfFile:YOUR_FILE_PATH];
}
else
{
UIActivityIndicatorView *act=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[imagView addSubview:act];
act.center=CGPointMake(imagView.frame.size.width/2, imagView.frame.size.height/2);
[act startAnimating];
__weak typeof(UIImageView) *weakImgView = imagView;
[imagView setImageWithURL:[NSURL URLWithString:REMOTE_FILE_PATH] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
for(UIView *dd in weakImgView.subviews)
{
if([dd isKindOfClass:[UIActivityIndicatorView class]])
{
UIActivityIndicatorView *act=(UIActivityIndicatorView *)dd;
[act stopAnimating];
[act removeFromSuperview];
}
}
NSString *extension=[YOUR_FILE_PATH pathExtension];
[self saveImage:image withFileName:YOUR_FILE_PATH ofType:extension];
}];
}
}
当我需要知道要显示的单元格的索引路径时,我有类似的情况。以 结束- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
。据推测,一个是“didEndDisplaying”,另一个是“willDisplayed”。