ios - app crash while deleting row from within uitableview cellForRowAtIndexPath delegate -
i have table view.i implementing functionality cells start fading(decrease alpha on time duration of 30 secs).after completing 30 secs, completion handler of view animation called delete row permanently data source(an array).all stuff goes in cellforrowatindexpath delegate method. problem in keeping in sync between array count before update , after update.
code :
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *postcellid = @"postcell"; uitableviewcell *cell = nil; nsuinteger row = [indexpath row]; cell = [tableview dequeuereusablecellwithidentifier:postcellid]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:postcellid] autorelease]; } post *currentpost = [posts objectatindex:row]; cell.textlabel.text = [currentpost posttitle]; cell.textlabel.font = [uifont systemfontofsize:14]; cell.detailtextlabel.text = [currentpost postdescr]; cell.detailtextlabel.font = [uifont systemfontofsize:10]; nstimeinterval duration = 30; [uiview animatewithduration:duration delay:0.0 options:uiviewanimationoptionbeginfromcurrentstate | uiviewanimationoptionallowuserinteraction animations:^ { cell.contentview.alpha = 0; } completion:^(bool finished) { [self.tableview beginupdates]; nslog(@"delete index >> %d array >> %@", row, posts); [posts removeobjectatindex:indexpath.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationnone]; [self.tableview endupdates]; }]; return cell; }
logs:
2015-06-04 07:22:05.764 feeder[1177:60b] delete index >> 0 array
( "", "", "", "", "" ) 2015-06-04 07:22:05.766 feeder[1177:60b] delete index >> 1 array >> ( "", "", "", "" ) 2015-06-04 07:22:05.767 feeder[1177:60b] delete index >> 2 array >> ( "", "", "" ) 2015-06-04 07:22:05.768 feeder[1177:60b] delete index >> 3 array >> ( "", "" )
if see last log, issue of crash.
crash log:
2015-06-04 07:22:05.770 feeder[1177:60b] * terminating app due uncaught exception 'nsrangeexception', reason: '* -[__nsarraym removeobjectatindex:]: index 3 beyond bounds [0 .. 1]'
how fix it.
your error keep old indexpath in block,and not update it.
for example: if have 2 records in array, action in complete block is
- delete row 0
- delete row 1
but,if delete first cell(row 0), row 1 indexpath.row update 0.but want delete 1.
so,i think can dynamic indexpath,then delete
[self.tableview beginupdates]; nsindexpath *path = [tableview indexpathforcell:cell]; nslog(@"delete index >> %d array >> %@", path.row, posts); [posts removeobjectatindex:path.row]; [self.tableview deleterowsatindexpaths:[nsarray arraywithobject:path] withrowanimation:uitableviewrowanimationnone]; [self.tableview endupdates];
i not sure if work,just try.
Comments
Post a Comment