【问题标题】:Swift 2: UICollectionView - is it possible to return cell asynchronously?Swift 2:UICollectionView - 是否可以异步返回单元格?
【发布时间】:2015-12-23 03:08:53
【问题描述】:

我在 UIView 容器中有一个 UICollectionView,我用它来显示附加到存储在 Parse 中的 PFObject 的图像(作为 PFFile 的数组)。我知道我可以在我当前的实现中同步获取 PFFile/图像(参见下面的同步加载工作代码),但我真的很想把它变成一个异步过程。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("attachedImageCell", forIndexPath: indexPath) as! AttachedImageCollectionViewCell

        if let uploadImageFile = attachedImageFiles[indexPath.row] as? PFFile {



            do {
                var imageData: NSData
                try imageData = uploadImageFile.getData()
                cell.imageView.image = UIImage(data: imageData)
            } catch let err as NSError {
                print("error getting image file data: \(err)")
            }

            /*
            //asynchronously getting image data - don't know how to return cell here
            uploadImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
                if error != nil {
                    //TODO: show error in download
                }
                if imageData != nil {
                    cell.imageView.image = UIImage(data: imageData!)
                }
            })
            */
        }

      return cell
    }

我一直在考虑的一种方法是让AttachedImageCollectionViewCell 对象观察其UIImageView,一旦将UIImage 文件(指针)分配给它,它就会从Parse 中获取PFFile 并解析它到NSData,用于UIImage。

由于我仍处于掌握 Swift 的学习曲线上,我不确定这种方法是否可行。 因此,我愿意倾听任何想法,谢谢!

【问题讨论】:

    标签: swift asynchronous parse-platform uiimage uicollectionview


    【解决方案1】:

    因此,在创建单元格的代码中,请确保使用库异步将图像设置为单元格-

    https://github.com/natelyman/SwiftImageLoader/blob/master/ImageLoader.swift

    //Create cell in usual way
    //Code here to create the cell 
    
    //Load the image needed by the cell asynchronously
    ImageLoader.sharedLoader.imageForUrl(urlString, completionHandler:{(image: UIImage?, url: String) in
        cell.image = image
    }) 
    

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 2014-07-02
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多