ios - 如何创建 pinterest 布局?

标签 ios swift swift3 uicollectionview

我正在尝试在我的应用程序中实现 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
}

最佳答案

我在我的项目中使用了相同的教程。我正在使用这个委托(delegate)函数。您需要导入 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
}

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

关于ios - 如何创建 pinterest 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459911/

相关文章:

ios - Amazon S3 iOS SDK v2 使用 AWSAccessKeyId :Signature 上传

ios - 使用 native Facebook 帐户(在设置中)登录 Facebook,无需网页 View

ios - 2 RACCommands 以便一个被禁用而另一个正在执行,反之亦然

在 iOS 自定义键盘中使用时,iOS CoreBluetooth 状态不受支持

iphone - 可以在任何 View 中截取屏幕截图的屏幕截图应用程序

arrays - Swift 上的 JSON : Array to String/TextField Text

ios - iOS 上的 UDP 广播/设备发现?

php - 无法读取数据,因为它的格式不正确 Xcode 11

selector - #selector'指的是没有暴露给Objective-C swift 3的方法

swift - UIButton 的阴影模糊了按钮图像