ios - Delete Multiple Rows from UITableView -
i have 2 methods delete rows uitableview, first 1 standard slide press delete, second deletes rows checked. problem when select more 1 or 2 crashes following in nslog
2014-04-08 22:37:53.985 punchslip[13128:60b] <nsindexpath: 0xc000000000038016> {length = 2, path = 0 - 7} 2014-04-08 22:37:53.987 punchslip[13128:60b] 7 2014-04-08 22:37:53.990 punchslip[13128:60b] <nsindexpath: 0xc000000000030016> {length = 2, path = 0 - 6} 2014-04-08 22:37:53.991 punchslip[13128:60b] 6 2014-04-08 22:37:53.993 punchslip[13128:60b] <nsindexpath: 0xc000000000028016> {length = 2, path = 0 - 5} 2014-04-08 22:37:53.995 punchslip[13128:60b] 5 2014-04-08 22:37:53.997 punchslip[13128:60b] <nsindexpath: 0xc000000000018016> {length = 2, path = 0 - 3} 2014-04-08 22:37:53.999 punchslip[13128:60b] 3 2014-04-08 22:37:54.001 punchslip[13128:60b] <nsindexpath: 0xc000000000020016> {length = 2, path = 0 - 4} 2014-04-08 22:37:54.003 punchslip[13128:60b] 4 2014-04-08 22:37:54.005 punchslip[13128:60b] *** terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfarray removeobjectatindex:]: index (4) beyond bounds (4)'
my first delete method looks this:
-(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if(editingstyle == uitableviewcelleditingstyledelete) { //delete row array , plist [arrayfromplist removeobjectatindex:indexpath.row]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *plistlocation = [documentsdirectory stringbyappendingpathcomponent:@"break.plist"]; [arrayfromplist writetofile:plistlocation atomically:yes]; [self.tableeview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationautomatic]; //[self.tableeview reloaddata]; } }
my second method, based off off first looks :
-(ibaction)erase:(id)sender{ nsarray *selectedcells = [self.tableeview indexpathsforselectedrows]; (nsstring *string in selectedcells) { nslog(@"%@", string); nsstring *stringone = [nsstring stringwithformat:@"%@", string]; nsstring *thisone = [[stringone componentsseparatedbystring:@" "] objectatindex:9]; nsstring *thatone = [[thisone componentsseparatedbystring:@"}"] objectatindex:0]; int rowint = [thatone intvalue]; nslog(@"%d", rowint); [arrayfromplist removeobjectatindex:rowint]; // [tableeview deleterowsatindexpaths:[nsarray arraywithobject:string] withrowanimation:uitableviewrowanimationautomatic]; [tableeview reloaddata]; } nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *plistlocation = [documentsdirectory stringbyappendingpathcomponent:@"break.plist"]; [arrayfromplist writetofile:plistlocation atomically:yes]; [self viewdidload]; }
the second method knows cells selected can see in log, when "too many" in case 4 says there nsrangeexeption.
any ideas on how correct it?
try second method,
-(ibaction)erase:(id)sender{ nsarray *selectedcells = [self.tableeview indexpathsforselectedrows]; nsmutableindexset *indicestodelete = [[nsmutableindexset alloc] init]; (nsindexpath *indexpath in selectedcells) { [indicestodelete addindex:indexpath.row]; } //arrayfromplist nsmutablearray [arrayfromplist removeobjectsatindexes:indicestodelete]; [tableeview beginupdates]; [tableeview deleterowsatindexpaths:selectedcells withrowanimation:uitableviewrowanimationautomatic]; [tableeview endupdates]; .... .... }
Comments
Post a Comment