2017-06-07 37 views
-1

我有下面的代码,我尝试删除行。当行不节最后一切正常,但是当它是最后一个部分 - 应用程序的崩溃任务:删除部分:)

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     if let item = shopList.getItem(index: indexPath) { 
      shopTableView.beginUpdates() 

      let indexSet = NSMutableIndexSet() 
      indexSet.add(indexPath.section - 1) 
      shopTableView.deleteRows(at: [indexPath], with: .fade) 
      //shopTableView.deleteSections(indexSet as IndexSet, with: UITableViewRowAnimation.automatic) 



      shopList.remove(item: item) 

      shopTableView.endUpdates() 

     } 
    } 
} 

我有错误

“NSInternalInconsistencyException”,理由是:“无效的更新:无效>部分的数量。更新(1)后,表视图>中包含的部分数量必须等于更新前的表格视图中包含的部分数量(2),加上或减去>插入或删除的部分数量(0插入,0删除)。'

有没有标准的方法来做到这一点?

回答

0

我改变了我下面的代码和它的作品

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     if let item = shopList.getItem(index: indexPath) { 
      shopTableView.beginUpdates() 
      let sectionStatus = shopList.remove(item: item) 
      shopTableView.deleteRows(at: [indexPath], with: .fade) 
      if sectionStatus == .sectionEmpty { 
       let indexSet = IndexSet(integer: indexPath.section) 
       shopTableView.deleteSections(indexSet, with: UITableViewRowAnimation.automatic) 
      } 
      shopTableView.endUpdates() 
      totalLabel.update(value: shopList.total) 

     } 
    } 

}