UITableView刪除cell導致崩潰的bug

2022-09-10 19:36:21 字數 830 閱讀 8500

該問題之前遇到過兩次,呼叫 deletesections: withrowanimation 方法時,刪除第乙個cell時正常,刪除第二個時就會崩潰,或者刪掉的不是自己想刪除的cell的問題,解決方法如下:

我的cell的刪除方法時在tableview的 - (uiview *)tableview:(uitableview *)tableview viewforfooterinsection:(nsinteger)section 方法中寫的,所以正常來說要刪除的話就是當前的section:

正常情況刪除應該是這樣:

[self.datasourcearray removeobject:model];

[self.tableview deletesections:[nsindexset indexsetwithindex:section] withrowanimation: uitableviewrowanimationtop];

但是這裡刪除方法裡不能直接用section,因為之前刪除時並沒有重新整理全域性的資料來源,所以導致這個section的數值還停留在之前的狀態,所以移除資料來源之前應該先取下當前實際的的section,所以應該下面這樣寫:

nsinteger index =[self.datasourcearray indexofobject:model];

[self.datasourcearray removeobject:model];

[self.tableview deletesections:[nsindexset indexsetwithindex:index] withrowanimation: uitableviewrowanimationtop];

UITableView 新增,刪除,編輯

uitableviewcell tableview uitableview tableview cellforrowatindexpath nsindexpath indexpath cell.textlabel.text clocknum objectatindex indexpath.row c...

UITableView 的行刪除

uitableview 編輯模式 created by mj on 13 4 11.import mjviewcontroller.h inte ce mjviewcontroller property nonatomic,retain nsmutablearray data end impleme...

UITableView 自帶編輯刪除

一 uitableview 自帶編輯刪除 1 實現兩個方法即可 pragma mark tableview自帶的編輯功能 void tableview uitableview tableview commiteditingstyle uitableviewcelleditingstyle editi...