添加时 PFQueryCollectionViewController 不显示新数据

PFQueryCollectionViewController not showing new data when added

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

我的应用程序目前可以很好地将照片推送到 Parse 服务器。但是,当我尝试在 "Wall".

上显示上传的照片时,我遇到了奇怪的行为

以下是我将照片保存到 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,使用 collectionView 的 performBatchUpdates 设置一个调用 reloadObjects 的按钮,但遇到了这个错误: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

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

在 cellForItemAtIndexPath 中放入:

    cell?.imageView.image = nil its work for me