ios - Deleting local file through UITableView causing out of bounds array -
ios - Deleting local file through UITableView causing out of bounds array -
i have uitableview on view controller has been set displays files have been downloaded documents of application. decided add together ability delete files through same table view. happens file deletes crashes afterwards. debugger states: terminating app due uncaught exception 'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 0 beyond bounds empty array'
here code delete button well:
// override back upwards editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { nserror *error; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; filepathsarray = [[nsfilemanager defaultmanager] subpathsofdirectoryatpath:documentsdirectory error:nil]; nsstring *currentfilename = [filepathsarray[indexpath.row] lastpathcomponent]; nsstring *documentsdirectorypath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filepath = [documentsdirectorypath stringbyappendingpathcomponent:currentfilename]; [filemgr removeitematpath:filepath error:&error]; } }
code table view:
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming [filepathsarray count]; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"maincell"]; if (cell == nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"maincell"]; } nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; filepathsarray = [[nsfilemanager defaultmanager] subpathsofdirectoryatpath:documentsdirectory error:nil]; cell.textlabel.text = [filepathsarray[indexpath.row] lastpathcomponent]; homecoming cell; }
so causing variable exceed? help appreciated!
i figured out! forgot remove object array , reload tableview, here working code wanting same:
// override back upwards editing table view. - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; filepathsarray = [[nsfilemanager defaultmanager] subpathsofdirectoryatpath:documentsdirectory error:nil]; nsstring *currentfilename = [filepathsarray[indexpath.row] lastpathcomponent]; nsstring *documentsdirectorypath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *filepath = [documentsdirectorypath stringbyappendingpathcomponent:currentfilename]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; [filemgr removeitematpath:filepath error:nil]; [filepathsarray removeobjectatindex:indexpath.row]; [tableview reloaddata]; } }
ios objective-c uitableview
Comments
Post a Comment