【问题标题】:how to create pinterest layout?如何创建 pinterest 布局?
【发布时间】:2018-07-05 16:59:14
【问题描述】:

我正在尝试在我的应用中实现 Pinterest 布局,我尝试了来自 raywenderlich 的教程。我的问题如何实现到 UICollectionView cellForItemAt 和 heightForPhotoAtIndexPath

这是我的代码:

   func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "GaleryCollection", for: indexPath) as! GaleryCollectionViewCell
    let imgGalery = self.galery![indexPath.row]
    if imgGalery.photo != nil {
        cell.imageGalery.af_setImage(withURL: URL(string: imgGalery.photo!)!)
    }
    return cell
}

【问题讨论】:

  • 查看 GitHub 以获取示例并从他们的代码中学习:swift / obj-c

标签: ios swift swift3 uicollectionview


【解决方案1】:

我在我的项目中使用了相同的教程。我正在使用这个委托功能。您需要导入 AVFoundation。如果您从数据库中提取图像,最好在数据库中设置图像的高度和宽度,以便像下面这样轻松调用它们。

func collectionView(collectionView: UICollectionView, heightForPhotoAt indexPath: IndexPath, with width: CGFloat) -> CGFloat {

    let postHeight = posts[indexPath.item].postImageHeight
    let postWidth = posts[indexPath.item].postImageWidth
    let postSize = CGSize(width: postWidth, height: postHeight)
    let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
    let rect = AVMakeRect(aspectRatio: postSize, insideRect: boundingRect)

    return rect.size.height
}

我现在在附加对象时遇到了一个问题。 :/

【讨论】:

    猜你喜欢
    • 2016-04-09
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多