2017-04-21 14 views
1

Core DataのNSManagedObjectから派生したタイプProductのオブジェクトを削除しようとしましたが、オブジェクトを正常に削除することができます。それはtableView.deleteRows(at:with:)の行でクラッシュしました。tableView.deleteRows(at:with :)毎回クラッシュしました

それで、クラッシュするたびに、私は再びアプリケーションを開きますが、オブジェクトは正常に削除されましたが、なぜそれがtableView.deleteRows(at:with:)にクラッシュしたのか分かりません。

どうすれば修正できますか?

class ProductListInSection { 
    let sectionName: String 
    var productsInSection: [Product] 

    init?(sectionName: String, productInSection: [Product]){ 
     guard !sectionName.isEmpty else { 
      return nil 
     } 
     self.sectionName = sectionName 
     self.productsInSection = productInSection 
    } 
} 


var categorySections: [ProductListInSection] 

// ... 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     let element = self.categorySections[indexPath.section].productsInSection[indexPath.row] 
     AppDelegate.viewContext.delete(element) 
     do { 
      try AppDelegate.viewContext.save() // successfully removed. 
     } catch { 
      print("Fail: \(error)") 
      return 
     } 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // crashed here. 
    } 
} 

次のエラーメッセージである:

2017年4月21日15:54:42.159 POS [20241:2852960] ***でアサーション障害 - [のUITableView _endCellAnimationsWithContext:]、/ BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UITableView.m:1737

答えて

4

オブジェクトを配列から削除するのを忘れました。 remove(at:)productsInSectionで使用し、配列からオブジェクトを削除してからdeleteRows(at:with:)を呼び出します。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     let element = self.categorySections[indexPath.section].productsInSection[indexPath.row] 
     AppDelegate.viewContext.delete(element) 
     do { 
      try AppDelegate.viewContext.save() // successfully removed. 
     } catch { 
      print("Fail: \(error)") 
      return 
     } 
     //Remove the object from array 
     self.categorySections[indexPath.section].productsInSection.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) // crashed here. 
    } 
} 
+0

ありがとうございました。私はとてもばかげている。 –

関連する問題