![](/img/trans.png)
[英]UICollectionView gets loading before delegate method call for web service
[英]UICollectionView Delegate method gets called before cell is instantiated?
我正在实现一个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。 我该怎么做才能正确设置此设置,以便在加载所有单元格后调用代理?
我在解析表视图中也有同样的感觉。 您对解析集合和表视图如何设置其尺寸绝对正确。 可以通过在上载图像时保存图像的宽度和高度来解决该问题。 这样,您就可以在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.