使用图像上传 Swift 动态调整 UICollectionViewCell 的大小 [英] Dynamically sizing UICollectionViewCell dynamically with image upload Swift

查看:52
本文介绍了使用图像上传 Swift 动态调整 UICollectionViewCell 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道动态调整 UICollectionViewCell 尺寸的最佳方法(Swift 3).我有异步图像上传到一个 ViewController,它为 UICollectionView 实现了适当的协议.图像的大小都不同,因此我需要在加载 UIImageView 数据后设置单元格尺寸.我对 collectionview 本身的布局选项持开放态度,但首先我想让单元格尺寸正确.

I'd like to know the best way to resize a UICollectionViewCell's dimensions dynamically(Swift 3). I have asynchronous image uploading to a ViewController which implements the appropriate protocols for UICollectionView. The images will all be different sizes, so I will need to set the cell dimensions after loading the UIImageView data. I'm open to options for the layout of the collectionview itself, but first I want to get the cell dimensions correct.

我知道已经有几篇类似的帖子了,但我想知道在 Swift 3 中执行此操作的最合适方法,但我还找不到正确的方法.

I know there are a few posts already that are similar, but I'd like to know the most appropriate way to do this in Swift 3 and I can't quite find the right way yet.

是否可以在 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> 中动态调整大小?UICollectionViewCell {} 方法?或者,是否可以在加载整个视图时设置自动调整大小选项?

Is it possible to do the resizing dynamically in the func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {} method? Or, is there an autoresize option I can set when loading the whole view?

这是我目前的相关方法,CollectionView 的所有静态大小:

Here's my relevant methods at the moment, all static sizing of the CollectionView:

override func viewDidLoad() {
        super.viewDidLoad()
        ExitNameLabel.text = exitName
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: 120 , height: 120)
        self.imagesCollectionView.setCollectionViewLayout(layout, animated: true)
        exitLocationDetails.text = exitLocation
        imagesCollectionView.delegate = self
        imagesCollectionView.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return imageUrls.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:ImageCollectionViewCell = imagesCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell

        if (images_cache[imageUrls[indexPath.row]] != nil) {
            cell.imageCell.image = images_cache[imageUrls[indexPath.row]]
        } else {
            loadImage(imageUrls[indexPath.row], imageview:cell.imageCell)
        }
        return cell
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let screenSize: CGRect = UIScreen.main.bounds
        let width = screenSize.width
        return CGSize(width: CGFloat(width), height: imagesCollectionView.bounds.size.height)
    }

感谢您帮助 iOS 新手 :)

Thanks for helping out an iOS newbie :)

推荐答案

试试这个

if (images_cache[imageUrls[indexPath.row]] != nil) {
    cell.imageCell.image = images_cache[imageUrls[indexPath.row]]
    cell.imageCell.contentMode = .scaleAspectFit
} else {
    loadImage(imageUrls[indexPath.row], imageview:cell.imageCell)
}

这篇关于使用图像上传 Swift 动态调整 UICollectionViewCell 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆