19

无论如何,在显示单元格时侵入 UICollectionView 以调用 willDisplayCell 委托方法?

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

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

4

4 回答 4

11

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

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
于 2014-10-08T16:21:47.130 回答
0

我正在做这样的事情cellForItemAtIndexPath:(用于延迟加载图像):

  • 如果我的模型有图像,只需将单元格的图像设置为它
  • 如果模型没有图像,则启动异步加载
  • 加载完成后,检查原始 indexPath 是否仍然可见
  • 如果是这样,请调用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;
}
于 2013-05-23T15:04:44.977 回答
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];
  }];
 }
}
于 2014-01-09T11:08:17.883 回答
0

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

于 2014-02-24T03:44:51.113 回答