为什么UICollectionView reloadData使我的应用程序崩溃?

时间:2015-05-31 11:32:36

标签: ios iphone swift uicollectionview reloaddata

我正在使用uicollectionview显示一些照片,我有一个按钮,允许用户删除所选照片。

除非我尝试删除用于填充uicollectionview的照片数组中的最后一张照片,否则它的效果非常好。即如果有5张照片,那么如果用户删除第5张照片而不是第1张,第2张,第3张或第4张,则会出现问题。当我尝试删除第5张照片时,它会在reloadData()上崩溃并出现以下错误

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'尝试从更新前仅包含4个项目的第0部分删除第4项

我不明白为什么会发生这种情况......错误甚至提到“试图删除”但我从未真正告诉它我正在删除任何内容。我只是更改了源,然后要求它更新。同样在错误消息中,它表示该部分在更新之前仅包含4个项目,而实际上是5张照片。为什么呢?

关于我正在做什么以及如何(使用Swift)的更多信息...

我有一个ProgressPhotoSheetClass,我已经从中实例化了对象progressPhotoSheet

此progressPhotoSheet对象包含一系列照片,照片可以包含照片ID,标题等优先级

在我使用的部分中的项目数

var numPics: Int = progressPhotoSheet.progressPhotos.count
return numPics

在我的cellForItemAtIndexPath中我使用

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoHolder
cell.photoTitle.text = progressPhotoSheet.progressPhotos[indexPath.row].photoName

...等等。

删除我使用的项目时

progressPhotoSheet.deletePhoto(progressPhotoSheet.progressPhotos [indexPath.row] .photoId)

从设备和我的核心数据中删除照片,然后使用修改过的核心数据重新加载progressPhotoSheet.progressPhotos,这样它现在就像以前一样,但没有删除的照片。

然后我打电话给

self.collectionView.reloadData()

哪个应该为新数据更新UICollectionView

我能理解,如果我使用集合视图中的内容与数据源中的内容之间存在不匹配     self.collectionView.deleteItemsAtIndexPaths(indexPaths) 因为那将被忽略以使它们匹配我们需要删除一个项目 - 这里有可能出现一些可能不匹配的东西..但是肯定使用self.collectionView.reloadData()无论做什么改变它应该只是看看现在有什么数据并相应地更新UICollectionView ....

所以我的问题是......为什么我会收到这个错误,我应该怎么做以解决问题,所以我不明白?

修改以包含更多信息

这是我的长焦代码

func deletePhoto(photoId: Int) {

    // Set up Core Data Managed Object Context
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext!



    // Fetch correct photo
    let fetchRequest = NSFetchRequest(entityName: "CDProgressPhoto")
    fetchRequest.predicate = NSPredicate(format: "photoId = %@", String(photoId))

    // Save
    if let fetchResults = managedContext.executeFetchRequest(fetchRequest, error: nil) as? [NSManagedObject] {
       if fetchResults.count != 0{

            // Will only be one photo with this photo id
            var photo = fetchResults[0]

            photo.setValue(true, forKey: "toDelete")

            // Save the object
            var error: NSError?
            if !managedContext.save(&error) {
                println("Could not save \(error), \(error?.userInfo)")
            }
        }
    }



    // Reload from core data
    self.loadPhotoSheetFromCoreData()
}

self.loadPhotoSheetFromCoreData()然后在从核心数据获取新数据之前清空progressPhotoSheet.progressPhotos ...下面的代码......

private func loadPhotoSheetFromCoreData() {

    if(self.hasPhotoSheet()) {

        // Clear existing photos
        self.progressPhotos = []

        // Set up Core Data Managed Object Context
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!

        let request = NSFetchRequest(entityName: "CDProgressPhoto")
        let predicate1 = NSPredicate(format: "photoSheetId == %@", String(self.sheetId))
        let predicate2 = NSPredicate(format: "toDelete == %@", false)
        request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false) as NSSortDescriptor]
        var predicatesArray: [NSPredicate] = [predicate1, predicate2]
        //predicatesArray.append(predicate1)
        request.predicate = NSCompoundPredicate.andPredicateWithSubpredicates(predicatesArray)
        let existings = managedContext.executeFetchRequest(request, error: nil)
        let existingPhotos: [CDProgressPhoto] =  existings as! [CDProgressPhoto]

        // for each photo make a ProgressPhoto object and add to progress photos array
        for photo in existingPhotos {
            var newPhoto: ProgressPhoto = ProgressPhoto()
            newPhoto.photoSheetId = Int(photo.photoSheetId)
            newPhoto.photoId  = Int(photo.photoId)
            newPhoto.photoName  = photo.photoName
            newPhoto.date = Int(photo.date)
            newPhoto.url = photo.url
            newPhoto.filename = photo.filename
            newPhoto.height = Float(photo.height)
            newPhoto.width = Float(photo.width)
            newPhoto.selected = false

            self.progressPhotos.append(newPhoto)
        }

    }
}

正如您所看到的那样,实际上没有删除照片我只是将toDelete标志设置为true,然后只重新加载toDelete设置为false的项目。照片将在以后根据网络连接等异步删除,因为它们也存储在服务器上以便在主网站上使用。

1 个答案:

答案 0 :(得分:-1)

你试过在collectionView上调用invalidateLayout()吗?这可能有助于你的观点是空的,即存在0个元素。

相关问题