0

I am trying to remove a item from my TableView but when I swipe to left and click in Delete, I got this error:

error when swipe

This is the code that I have, when I comment the self.tableView.deleteRowsAtIndexPaths line, everything works fine for me.

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            if let index: NSIndexPath = indexPath {
                self.tableView.beginUpdates()

                self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
                loadProductsToListOrder()

                self.tableView.endUpdates()
            }
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }

I searched and found here on stack overflow about this beginUpdates and endUpdates.

Thank you.

Update

loadProductsToListOrder function

func loadProductsToListOrder() {
        self.products = ProductDatabase.listProduct(self.configurations.db!)

        self.tableView.reloadData()
    }

If I have one element in my TableView, I do not have this problem. Just happen if I have more than one itens in my TableView.

Update 2

The answer that @harshayarabarla gave works for me, because I was forgetting to add the self.products.removeAtIndex(indexPath.row) before the deleteRowsAtIndex.

Thanks!

Felipe Umpierre
  • 188
  • 2
  • 12

2 Answers2

0

See Delete Data from Coredata Swift for full history.

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        switch editingStyle {
        case .Delete:
            // remove the deleted item from the model
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext!
            context.deleteObject(myData[indexPath.row] as NSManagedObject)
            myData.removeAtIndex(indexPath.row)
            context.save(nil)

           //tableView.reloadData()
            // remove the deleted item from the `UITableView`
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        default:
            return

        }
}

Also need was these two lines of code to be corrected.

    var myData: Array<AnyObject> = []
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
Community
  • 1
  • 1
MwcsMac
  • 6,810
  • 5
  • 32
  • 52
0

You need to remove the object from your datasource of the table view before you perform self.tableView.deleteRowsAtIndexPaths. later you can call self.tableView.reloadData method instead of begin updates and end updates. It's not necessary to call reloadData method though, as self.tableView.deleteRowsAtIndexPaths takes care of reloading thing.

if you call self.tableView.deleteRowsAtIndexPaths before making changes to dataSource of the table view you might face this error.

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

                self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
                self.products.removeAtIndex(index.row)
                self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
                loadProductsToListOrder() // you can comment this if you are not adding any new items to products array
            }
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
harsha yarabarla
  • 506
  • 4
  • 11