2015-11-05 23 views
0
刪除項目

我想從我的TableView刪除項目,但是當我向左滑動,並在單擊刪除,我得到這個錯誤:AppDelegate的錯誤,當從TableView中

error when swipe

這是代碼,我有,當我評論self.tableView.deleteRowsAtIndexPaths線,一切工作正常。

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 
     } 
    } 

我搜查,發現這裏堆棧溢出這個beginUpdatesendUpdates

謝謝。

更新

loadProductsToListOrder功能

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

     self.tableView.reloadData() 
    } 

如果我在我的TableView一個元素,我沒有這個問題。如果我在TableView中有多個itens,就會發生。

更新2

是@harshayarabarla給了作品對我來說,因爲我忘記deleteRowsAtIndex前添加self.products.removeAtIndex(indexPath.row)答案。

謝謝!

+0

你的應用程序崩潰了!你有沒有得到任何崩潰日誌?把異常斷點看看它在哪裏崩潰 –

+0

顯示方法加載產品!!! –

+0

FUNC loadProductsToListOrder(){ self.products = ProductDatabase.listProduct(self.configurations.db!) self.tableView.reloadData() } –

回答

0

您需要在執行self.tableView.deleteRowsAtIndexPaths前,從表視圖的數據源中刪除的對象。稍後您可以調用self.tableView.reloadData方法,而不是開始更新和結束更新。雖然沒有必要調用reloadData方法,因爲self.tableView.deleteRowsAtIndexPaths負責重新加載事物。

如果在更改表視圖的dataSource之前調用self.tableView.deleteRowsAtIndexPaths,則可能會面臨此錯誤。

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 
     } 
    } 
0

查看Delete Data from Coredata Swift的完整歷史記錄。

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 

     } 
} 

還需要將這兩行代碼加以糾正。

var myData: Array<AnyObject> = [] 
let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext