ios - 添加时 PFQueryCollectionViewController 不显示新数据

标签 ios swift parse-platform uicollectionview

我一直在探索如何创建允许用户共享照片的社交应用程序。我使用 Parse 作为后端,该应用程序的功能之一是允许用户将图片保存到 Parse,并查看当前保存到 Parse 的所有照片。

我的应用目前可以很好地将照片推送到 Parse 服务器。但是,当我尝试在我的“墙”上显示上传的照片时,我遇到了奇怪的行为。

以下是我将照片保存到 Parse 的方式:

let pictureData = UIImagePNGRepresentation(UIImage(named: "someImage"))
let file = PFFile(name: "image", data: pictureData)
file.saveInBackgroundWithBlock({ succeeded, error in
    if succeeded {
        let wallObject = WallObject(imageFile: file)
        wallObject.saveInBackgroundWithBlock { succeeded, error in
            if succeeded { println("Succeeded saving picture to Parse") }
            if let error = error { println("Error saving object: \(error)") }
        }
    } else {
        println("Error saving file to Parse: \(error)")
    }
})

保存工作正常,当我检查网站上的 Parse 数据库时,图像就在那里。但是,当我尝试在我的 PFQueryCollectionViewController 子类中显示图像时(通过使用 pullDownToRefresh),新图像不会将自身作为新单元格插入,而是替换其中一张旧图像,踢collectionView 中最旧的图像,同时保留相同数量的单元格,即使在从 Parse 中获取新对象后也是如此。

除了子类化 UICollectionViewLayout 和做一些布局代码外,我还对 PFQueryCollectionViewController 进行了最低限度的设置。通过 objectsWillLoadobjectsDidLoad 方法中的 println(objects.count) 语句,我确信新对象已经加载到 objects 数据数组。

解决方案的尝试: 我尝试了 collectionView!.reloadData,设置了一个调用 reloadObjects 的按钮,使用 collectionView 的 performBatchUpdates 并遇到了这个错误:UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:

底线:尽管 PFQueryCollectionViewController 在运行时刷新时识别了新对象,但它不会插入新单元格来容纳新项目。

需要注意的是,当 Parse 数据库完全为空并且将新图片上传到 Parse 时,collectionView 可以为新图像正确生成 1 个新 Cell。


PFQueryCollectionViewController 数据源实现

  override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFCollectionViewCell? {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as? FeedViewCollectionCell
    if cell == nil {
      cell = FeedViewCollectionCell()
    }
    cell!.populateDataWithObject(object as! CollectionCellData)
    return cell
  }

最佳答案

Aside from subclassing UICollectionViewLayout and doing some layout code

这很可疑,您是否在没有自定义布局的情况下尝试过?

关于ios - 添加时 PFQueryCollectionViewController 不显示新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31619992/

相关文章:

ios - 使用 Alamofire、ImageLoader 在 swift 中解析 Json 来解析键值以及具有图像的 TableView 中的对象

ios - 错误 : 'Type of expression is ambiguous without more context'

ios - 新版本在上传到 iTunes Connect 后消失

iphone - Objective-C 中的 NSMutableArray 插入

swift - 在前台而不是后台接收 Firebase 通知

swift - 核心动画,Swift : how to animate NSWindow

javascript - 使用顺序 for 循环解析多链 Promise

javascript - Parse.com 在云功能上设置 ACL

ios - 如何在返回值之前等待关闭完成

ios - 在 Swift for iOS 中沿圆形路径绘制文本