2017-04-20 112 views
0

我知道這個問題上有很多帖子,我已經通過其中大部分帖子,但我仍然無法解決我刪除tableViewCell時導致我的應用崩潰的問題遵循錯誤:刪除UITableViewCell導致崩潰

Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})'

我明確地把我行使用

tableView.rowHeight = 140

我的代碼刪除如下

高度
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in 
     let object = self.datastore[indexPath.row] 

     do { 
      try self.realm?.write { 
       self.realm?.delete(object) 

        self.datastore.remove(at: indexPath.row) 
        self.tableView.deleteRows(at: [indexPath], with: .none) 
      } 

     } catch let error { 

     } 
    } 
    deleteAction.backgroundColor = .red 

    return [deleteAction] 
} 

這確實從dataStore中刪除了該項目。我已經嘗試了許多不同的解決方案,比如調用mainThread的delete,重載整個dataSource等,但它仍然崩潰。

我的numberOfRowsInSection方法被調用並且在刪除後返回正確的行數,所以看起來不是問題。

添加一般的異常斷點雖然我認爲它在這裏轟然這裏

 func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? Cell { 
     cell.image.kf.cancelDownloadTask() 
    } 
} 

KF是翠鳥庫,我想取消圖像下載,如果該單元被刪除

我所注意到的是如果我用

`tableView.dequeueReusableCell(withIdentifier: "Cell")` 

,而不是 tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

這不會崩潰,所以這可能是我的解決方案,但我很想知道這裏有什麼問題。我明白'forIndexPath'方法應該總是返回一個單元格,但在這個例子中由於nil而崩潰。

我在另一個的viewController相同刪除代碼,不依賴於didEndDisplaying:細胞的方法,這不會崩潰

+0

嘗試改變優先級: self.tableView.deleteRows(在:indexPath],其中:.none) self.datastore.remove(在:indexPath.row) – KKRocks

+0

這不會爲你工作必須在執行刪除操作之前編輯您的數據源,否則會導致無效的更新異常編輯導致錯誤的計數**事實上,我剛剛確認這不起作用 – TommyBs

+0

您的代碼在哪行停止了? – KKRocks

回答

0

您需要更改didEndDisplaying此行。你需要獲得現有的細胞。

let cell = tableView.cellForRowAtIndexPath(indexPath)