ios UICollectionView willDisplayCell 委托方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13237102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView willDisplayCell delegate method?
提问by Devfly
Anyway to hack into UICollectionView to call willDisplayCell delegate method, when displaying cell?
无论如何,在显示单元格时进入 UICollectionView 调用 willDisplayCell 委托方法?
I need this for lazy loading, and I'm doing it nice with UITableView, but officially UICollectionView doesn't have that kind of delegate method.
我需要这个来延迟加载,而且我在 UITableView 上做得很好,但正式的 UICollectionView 没有那种委托方法。
So, any ideas? Thanks!
那么,有什么想法吗?谢谢!
采纳答案by Eli Burke
FYI, This has been added to the API for iOS 8:
仅供参考,这已添加到 iOS 8 的 API 中:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
回答by DeepFriedTwinkie
I'm doing something like this in cellForItemAtIndexPath:
(for lazy loading images):
我正在做这样的事情cellForItemAtIndexPath:
(用于延迟加载图像):
- If my model has the image, just set the cell's image to it
- If the model does not have an image, kick of an async load
- When the load completes, the check if the original indexPath is still visible
- If so, call
cellForItemAtIndexPath
for the original indexPath. Since the image now exists in the model, the cell's image will now be set.
- 如果我的模型有图像,只需将单元格的图像设置为它
- 如果模型没有图像,则启动异步加载
- 加载完成后,检查原始 indexPath 是否仍然可见
- 如果是这样,请调用
cellForItemAtIndexPath
原始 indexPath。由于图像现在存在于模型中,现在将设置单元格的图像。
Looks like this:
看起来像这样:
- (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;
}
回答by Ajith Tom
You can make use of SDWebImage
https://github.com/rs/SDWebImagefor lazy loading. You can use this effectively with UICollectionView
.
您可以使用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];
}];
}
}
回答by BabyPanda
I have a similar situation when I need to know the index path of cell which is going to be displayed. Ended with - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
. Assumably, one is "didEndDisplaying", another is "willDisplayed".
当我需要知道将要显示的单元格的索引路径时,我遇到了类似的情况。以- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
. 据推测,一个是“didEndDisplaying”,另一个是“willDisplayed”。