ios - indexpath not removing in UITableView -


i have uitableview cell display uitextview text. need delete cell rows text using uitableviewcelleditingstyledelete. when use edit button delete text got error.

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {      nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]];      // nslog(@"array %@",mymutablearrayagain);      return [mymutablearrayagain count];  } 

delete function:

-(void)editbuttonpressed:(uibutton *)button{      nslog(@">>> entering %s <<<", __pretty_function__);      [tableview setediting:![tableview isediting] animated:yes];      nsstring *buttontitle = ([tableview isediting]) ? @"done" : @"edit";      [editbutton settitle:buttontitle forstate:uicontrolstatenormal];      nslog(@"<<< leaving %s >>>", __pretty_function__); }   - (void)tableview:(uitableview *)tableview1 commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {     nslog(@">>> entering %s <<<", __pretty_function__);       nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]];       if (editingstyle == uitableviewcelleditingstyledelete)     {          //first delete db of favorites table         nslog(@"art id %@",[mymutablearrayagain objectatindex:indexpath.row]);              nslog(@"indexpath %d", indexpath.row);          [mymutablearrayagain removeobjectatindex:indexpath.row];             nslog(@"remove %d",indexpath.row);              nsarray *indexpathstoremove = [nsarray arraywithobject:indexpath];          nslog(@"indextoremove %@",indexpathstoremove);          [tableview1 deleterowsatindexpaths:indexpathstoremove withrowanimation:uitableviewrowanimationright];      }      nslog(@"<<< leaving %s >>>", __pretty_function__); }    - (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)fromindexpath       toindexpath:(nsindexpath *)toindexpath {     nslog(@">>> entering %s <<<", __pretty_function__);      nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]];       nsstring *contentstomove = [[mymutablearrayagain objectatindex:[fromindexpath row]] retain];      [mymutablearrayagain removeobjectatindex:[fromindexpath row]];      [mymutablearrayagain insertobject:contentstomove atindex:[toindexpath row]];      [contentstomove release];      nslog(@"<<< leaving %s >>>", __pretty_function__); } 

error in nslog:

2013-10-07 14:29:41.939 worksa[3689:c07] indexpath 0 2013-10-07 14:29:41.940 worksa[3689:c07] remove 0 2013-10-07 14:29:41.940 worksa[3689:c07] indextoremove (     "<nsindexpath 0x8b44fa0> 2 indexes [0, 0]" ) 2013-10-07 14:29:41.941 worksa[3689:c07] *** assertion failure in -[uitableview _endcellanimationswithcontext:], /sourcecache/uikit_sim/uikit-1912.3/uitableview.m:1046 2013-10-07 14:29:41.942 worksafeact[3689:c07] *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0.  number of rows contained in existing section after update (7) must equal number of rows contained in section before update (7), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).' 

enter image description here

you need add synchronize statement after deleting, change function shown below.

- (void)tableview:(uitableview *)tableview1 commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {     nslog(@">>> entering %s <<<", __pretty_function__);     nsmutablearray* mymutablearrayagain = [nsmutablearray arraywitharray:[[nsuserdefaults standarduserdefaults] objectforkey:@"save"]];     if (editingstyle == uitableviewcelleditingstyledelete)     {         //first delete db of favorites table         nslog(@"art id %@",[mymutablearrayagain objectatindex:indexpath.row]);         nslog(@"indexpath %d", indexpath.row);         [mymutablearrayagain removeobjectatindex:indexpath.row];          //add these 2 lines - need in moving items.         [[nsuserdefaults standarduserdefaults] setobject:mymutablearrayagain forkey:@"save"];         [[nsuserdefaults standarduserdefaults] synchronize];         //add these 2 lines          [mymutablearrayagain removeobjectatindex:indexpath.row];         nslog(@"remove %d",indexpath.row);         nsarray *indexpathstoremove = [nsarray arraywithobject:indexpath];         nslog(@"indextoremove %@",indexpathstoremove);         [tableview1 deleterowsatindexpaths:indexpathstoremove withrowanimation:uitableviewrowanimationright];      }      nslog(@"<<< leaving %s >>>", __pretty_function__); } 

also can use following code redefine nslog , have print variables or strings give method names , all, define these anywhere in appdelegate

#ifdef debug #   define nlog(fmt, ...) nslog((@"%s [line %d] " fmt), __pretty_function__, __line__, ##__va_args__) #endif 

check more information on nslog formatting


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -