如何在 Swift 2.0 中滑动删除核心数据表视图 [英] How to swipe delete core data tableview in Swift 2.0

查看:28
本文介绍了如何在 Swift 2.0 中滑动删除核心数据表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VC 中有一个表视图.它使用以下代码从核心数据对象填充:

I have a table view within a VC. It is populated from a core data object using the following code:

// Variables
var allFruits: NSArray
var managedObjectContext: NSManagedObjectContext
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

func loadFruits(){
    // Try to load all of the Fruits from the core data.
    let fetchRequest = NSFetchRequest()

    let entityDescription = NSEntityDescription.entityForName("Fruit", inManagedObjectContext: self.managedObjectContext)

    fetchRequest.entity = entityDescription
    do{
        self.allFruits = try self.managedObjectContext.executeFetchRequest(fetchRequest)

        if self.allFruits.count == 0{
            print("No saved Fruits")
        }
    }
    catch
    {
        let fetchError = error as NSError
        print(fetchError)
    }
}   

然后用这个特定的水果数据填充表格视图.我有这个方法可以从表中删除水果

Then the table view is populated with this specific fruit data. I have got this method for deletion of the Fruits from the table

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
         // handle delete (by removing the data from your array and updating the tableview)
         managedObjectContext.deleteObject(allFruits[indexPath.row] as! NSManagedObject)

         // Attempt to save the object
         do{
             try appDelegate.managedObjectContext.save()
         }
             catch let error{
             print("Could not save Deletion \(error)")
         }

         // Remove the deleted item from the table view
         self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

         // Reload the fruits
         self.loadFruits()

         // Reload the table view
         tableView.reloadData()
    }
}

每次我尝试删除水果时,这只会使应用程序崩溃.

This instead just crashes the app every time I try to delete the Fruits.

'NSInternalInconsistencyException',原因:'无效更新:无效第 0 节中的行数.更新后的现有部分 (5) 必须等于更新前包含在该节中的行 (5),加号或减号从该部分插入或删除的行数(插入 0,1 删除)并加上或减去移入或移出的行数该部分(0 移入,0 移出).'

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我怀疑我使用的是 NSArray 而不是 NSMutableArray 的事实存在一些问题.

I am suspecting that there is some issue with the fact that I am using an NSArray as opposed to an NSMutableArray.

我该如何解决这个问题?

How can I fix this issue?

提前致谢.

推荐答案

我通过简单地更改此方法来修复我的代码:

I fixed my code by simply changing this method:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {


             // handle delete (by removing the data from your array and updating the tableview)
             managedObjectContext.deleteObject(allFruits[indexPath.row] as! NSManagedObject)

             //Attempt to save the object
             do{
             try appDelegate.managedObjectContext.save()
             }
             catch let error{
             print("Could not save Deletion \(error)")
             }

             //Reload the fruits
             self.loadFruits()

             //Reload the table view
             tableView.reloadData()


        }
    }

这成功地从核心数据中删除了对象并更新了表视图.

This successfully deletes the object from the core data as well as updating the table view.

这篇关于如何在 Swift 2.0 中滑动删除核心数据表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆