【发布时间】:2018-06-25 03:14:47
【问题描述】:
您好,我正在尝试让单元格高度作为图像高度
我正在使用 PinterestLayout,一切正常,但问题是在获取图像高度之前返回 heightForPhotoAtIndexPath
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat {
// Get Object
let images = addArray[indexPath.row]["images"] as! [PFFile]
let image = images[0] as PFFile
image.getDataInBackground(block: { (data, error) in
if error == nil { if let imageData = data {
self.image2 = UIImage(data: imageData)!
self.height = self.image2.size.height
if self.height > 1000.0 {
self.height = self.height / 4.0
} else if self.height > 450 {
self.height = self.height / 3.0
}
print(self.height)
}}})
print(height)
return height
}
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 281.0 609.0 250.0 197.666666666667 281.0 281.0 281.0 281.0 281.0
【问题讨论】:
-
我建议不要在
collectionview委托方法中进行这种获取,而是在不同的方法中进行此调用并将高度存储在某些结构中(例如在数组中)然后使用那些做你想做的工作的高度。 -
喜欢这种方法吗? func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return cellSize }
-
如果图片是异步下载的,那么你无法提前知道它的大小,所以你最好的方法是估计大小,一旦下载完成。但是,这可能不会带来最佳体验。或者,如果服务器支持它,您可以请求具有所需大小的图像。
标签: ios swift uitableview parse-platform