UICollectionView willDisplayCell委托方法?

时间:2012-11-05 17:19:52

标签: iphone ios xcode cocoa-touch uicollectionview

无论如何要破解进入UICollectionView来调用willDisplayCell委托方法,何时显示单元格?

我需要这个用于延迟加载,而且我用UITableView做得很好,但官方的UICollectionView没有那种委托方法。

那么,有什么想法吗?谢谢!

4 个答案:

答案 0 :(得分:10)

仅供参考,这已添加到iOS 8的API中:

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);

答案 1 :(得分:0)

我在cellForItemAtIndexPath:中做了类似的事情(对于延迟加载图片):

  • 如果我的模型有图像,只需将单元格的图像设置为
  • 如果模型没有图像,则执行异步加载
  • 加载完成后,检查原始indexPath是否仍然可见
  • 如果是,请为原始indexPath调用cellForItemAtIndexPath。由于图像现在存在于模型中,因此现在将设置单元格的图像。

看起来像这样:

- (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;
}

答案 2 :(得分:0)

您可以使用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];
  }];
 }
}

答案 3 :(得分:0)

当我需要知道将要显示的单元格的索引路径时,我有类似的情况。结束于- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath。可能,一个是“didEndDisplaying”,另一个是“willDisplayed”。