2016-11-11 29 views
0

我已經看到了很多話題關於這個問題,但我仍然有錯誤...應用程序崩潰時,我想用tableview.deleteRows(在:與:)

這是我在的tableview委託方法代碼

self.tableView.beginUpdates() 

if self.data[sectionType]!.isEmpty { 
    self.data.removeValue(forKey: sectionType) 
} 

self.tableView.deleteRows(at: [indexPath], with: .fade) 
self.tableView.endUpdates() 

我已經實現了numberOfSections委託方法是這樣的::(當用戶刷卡並點擊單元上的刪除按鈕被調用)

func numberOfSections(in tableView: UITableView) -> Int { 
    return data.count 
} 

這是錯誤我得到:

***終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「內部的UITableView錯誤:無法生成與老款數的新的部分地圖:1,新區段計數:0

我得到同樣的錯誤,如果我通過reloadData()替換deleteRows(在tableView中)方法...

我用斷點檢查,我在錯誤發生時輸入if條件。所以數據被更新(removeValue()調用後的0個元素),我調用了deleteRows方法。

感謝您的幫助。

+2

您是否使用了numberOfRowsInSection方法? –

回答

0

你想刪除一行或一節嗎?

if self.data[sectionType]!.isEmpty { 
    self.data.removeValue(forKey: sectionType) 
} 

建議您試圖刪除一節。如果你是它應遵循這一類的東西:

let indexSet = NSMutableIndexSet() 
indexSet.addIndex(section) 
self.tableView.deleteSections(indexSet, withRowAnimation: .fade) 

如果你想刪除一行,則頂部更改到這樣的事情:

if self.data[sectionType]!.isEmpty { 
    self.data[sectionType].remove(at: rowToRemove) 
} 
+0

你完全正確!我想刪除一個部分。我沒有使用正確的UITableView方法。 deleteSections()方法修復了我的問題:) 謝謝! –

0

看起來你可能刪除部分中的所有行,而不是一個單元格。

我假設self.data是一個字典,其中的鍵是節,值是包含單元格信息的數組。 而不是刪除字典的關鍵。就像你在這裏...

self.data.removeValue(forKey: sectionType) 

你可能想要訪問該部分並刪除該行。事情是這樣的......

data[sectionType]?.remove(at: indexPath.row) 

我假設你有方法numberOfRowsInSection:返回值是類似的東西...

return data[sectionType]?.count 

希望這有助於以某種方式。