2017-05-29 44 views
-1

雨燕3.0的iOS 10.x的NSInternalInconsistencyException的tableView行刪除

使用此代碼,試圖刪除表中的行...

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     print("DELETE \(indexPath)") 
     self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 


    } 
} 

這失敗,出現錯誤信息?

2017-05-29 13:36:23.843228+0200[939:576777] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), 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).' 

這聽起來不錯,只有這是鍋爐板代碼?我只是點擊紅色按鈕而已?

enter image description here

是的,有3列在這裏......在我的代碼墜毀有9

有什麼我錯過這裏?這裏打印出返回的indexPath,確實是錯的,但是等一下我沒有設置它。這種方法呢?

刪除[0,3]

+0

你需要在數據模型中刪除行以及刪除之前,必須先刪除你的數據陣列行。請發佈您的'cellForRow'的實現,以便我可以幫到您 –

+2

您錯過了從數據源數組中刪除該項目(**調用'deleteRows(at')之前**。 – vadian

回答

1

tableView

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     print("DELETE \(indexPath)") 
     yourArray.remove(at: indexPath.row) /* delete in data array */ 
     self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
} 
相關問題