OGeek|极客世界-中国程序员成长平台

标题: ios - 在实例化单元格之前调用 UICollectionView 委托(delegate)方法? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:51
标题: ios - 在实例化单元格之前调用 UICollectionView 委托(delegate)方法?

我正在实现一个 UICollectionView,它通过 Parse 从在线获取数据。我正在将图像加载到 Collection View 中。我遇到的问题是 Collection View 的委托(delegate)方法之一在重新加载单元格之前被调用。

我有一个方法可以将背景中的对象加载到 NSArray 中。一旦他们都完成了,我重新加载我的收藏 View 。从那里我的委托(delegate)方法在 collectionView:cellForItemAtIndexPath 之前被调用。我的委托(delegate)方法处理每个单独单元格的大小,以便它们可以根据照片的大小进行更改。

这是一个问题,因为单元格中需要包含图像才能让我看到它们对于委托(delegate)方法的大小。

我的委托(delegate)方法如下所示:

- (CGSize)collectionViewUICollectionView *)collectionView layoutNHBalancedFlowLayout *)collectionViewLayout preferredSizeForItemAtIndexPathNSIndexPath *)indexPath
{   
    CustomCollectionViewCell *currentCell = (CustomCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    if (currentCell) {
        return [currentCell.cellImage.image size];
    }
    return [cellImage size];
}

就像我说的,这个委托(delegate)方法在 collectionView:cellForItemAtIndexPath 之前被调用。这导致局部变量“currentCell”返回零。我该怎么做才能正确设置它,以便在加载所有单元格后调用委托(delegate)?



Best Answer-推荐答案


我在 Parse TableView 中可能也遇到了同样的情况。关于解析集合和表格 View 如何设置它们的尺寸,您是绝对正确的。上传图片的时候保存图片的宽高可以解决这个问题。这样,您可以在 PFFile 图像完成下载之前获取图像的尺寸(这通常发生在设置 Collection View 之后)。

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath {

    MyParseImage *image = [self.objects objectAtIndex:indexPath.row];
    CGFloat imageHeight = [[image objectForKey:height] floatValue];
    CGFloat imageWidth = [[image objectForKey:width] floatValue];
    CGFloat ratio = self.view.frame.size.width / imageWidth;

    return imageHeight * ratio;
}

关于ios - 在实例化单元格之前调用 UICollectionView 委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22160903/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4