ios - reorder and save UITableView -
i parse data server contains passengers details. passengers details saved passenger class. main viewcontroller contains uitableview loads passengers data , allows user rearrange cells order.
my question how can save tables new order , load again each time app starts, new passenger details parsed server. prefer not use core data.
here code:
passenger.h
#import <foundation/foundation.h> @interface passenger : nsobject { nsstring *name; nsstring *code; nsstring *country; nsstring *date; } @property (nonatomic, strong) nsstring *name; @property (nonatomic, strong) nsstring *code; @property (nonatomic, strong) nsstring *country; @property (nonatomic, strong) nsstring *date;
mainviewcontroller m
- (void)tableview:(uitableview *)tableview moverowatindexpath:(nsindexpath *)sourceindexpath toindexpath:(nsindexpath *)destinationindexpath { nsstring *stringtomove = passengersarray[sourceindexpath.row]; [passengersarray removeobjectatindex:sourceindexpath.row]; [passengersarray insertobject:stringtomove atindex:destinationindexpath.row]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } passenger *current = [passengersarray objectatindex:indexpath.row]; uilabel *namelabel = (uilabel *)[cell viewwithtag:101]; namelabel.text = current.name; uilabel *codelabel = (uilabel *)[cell viewwithtag:102]; codelabel.text = current.code; uilabel *countrylabel = (uilabel *)[cell viewwithtag:103]; countrylabel.text = current.country; return cell; }
you can load passenger list plist in form of array. , when update information iterate through plist array, compare, update , add new passengers list. once user has rearranged order save plist.
fetch
nspropertylistformat format; nsstring *errordesc; nsstring * plistpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes) objectatindex:0]; plistpath = [plistpath stringbyappendingpathcomponent:@"passengers.plist"]; nsdata *plistxml = [[nsfilemanager defaultmanager] contentsatpath:plistpath]; nsarray *temp = (nsarray *)[nspropertylistserialization propertylistfromdata:plistxml mutabilityoption:nspropertylistmutablecontainersandleaves format:&format errordescription:&errordesc];
save
nsstring *error; nsstring *plistpath; nsstring *rootpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes) objectatindex:0]; plistpath = [rootpath stringbyappendingpathcomponent:@"passengers.plist"]; nsdata *plistdata = [nspropertylistserialization datafrompropertylist:_passengersarray format:nspropertylistxmlformat_v1_0 errordescription:&error]; if(plistdata) { [plistdata writetofile:plistpath atomically:yes]; } else { nslog(@"error writing passengers file %@", error); }
Comments
Post a Comment