【问题标题】:cell height as image height单元格高度作为图像高度
【发布时间】: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


【解决方案1】:

在我之前的项目中,我使用了 Pinterest Layout,并在下面使用了这种方法。但我不记得细节了。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let itemSize = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right + 10)) / 2
    return CGSize(width: itemSize, height: itemSize)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2017-07-19
    相关资源
    最近更新 更多