在实例化单元格之前调用UICollectionView Delegate方法?

时间:2014-03-04 01:07:31

标签: ios iphone objective-c uicollectionview parse-platform

我正在努力实现一个UICollectionView,它通过Parse从在线获取数据。我正在将图像加载到集合视图中。我遇到的问题是在重新加载单元格之前调用了集合视图的一个委托方法。

我有一个方法可以将背景中的对象加载到NSArray中。完成所有操作后,我会重新加载我的集合视图。从那里我的委托方法在collectionView:cellForItemAtIndexPath之前被调用。我的委托方法处理每个单元格的大小,以便它们可以根据照片的大小进行更改。

这是一个问题,因为单元格需要在其中包含图像,以便我查看它们对委托方法的大小。

我的委托方法如下所示:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(NHBalancedFlowLayout *)collectionViewLayout preferredSizeForItemAtIndexPath:(NSIndexPath *)indexPath
{   
    CustomCollectionViewCell *currentCell = (CustomCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

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

就像我说的那样,这个委托方法是在collectionView:cellForItemAtIndexPath之前调用的。这导致局部变量'currentCell'返回nil。我能做什么才能正确设置,以便在我的所有单元格都加载后调用代表?

1 个答案:

答案 0 :(得分:1)

我可能与Parse表视图一样。关于解析集合和表格视图如何设置其维度,你是完全正确的。上传图像时,可以通过保存图像的宽度和高度来解决此问题。通过这种方式,您可以在之前抓取图像的尺寸完成PFFile图像的下载(这通常发生在设置集合视图之后)。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)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;
}