2015-12-23 2 views
0

У меня есть UICollectionView в контейнере UIView, и я использую его для отображения изображений (в виде массива PFFile), прикрепленного к PFObject, хранящегося в Парсе. Я знаю, что я мог бы получить PFFile/изображение синхронно в моей текущей реализации (см. Ниже рабочий код для синхронной загрузки), но я бы очень хотел сделать это асинхронным процессом.Swift 2: UICollectionView - возможно ли вернуть ячейку асинхронно?

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 (указатель) назначается к нему, он будет идти Извлечение в PFFile из анализа и разобрать его NSData , который используется для UIImage.

Поскольку я все еще нахожусь на кривой обучения овладения Свифт, я не уверен, насколько это возможно. Так что я все уши для любых идей здесь, спасибо!

ответ

1

Так что в вашем коде, где вы создаете ячейку, вы убедитесь, установить изображение в ячейку асинхронно с помощью Library-

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

Смежные вопросы