iphone - retrieve data from NSUserDefaults to TableView -
i save values of 2 labels through nsuserdefaults :
- (ibaction) savedata { // store data nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:autore.text forkey:@"author"]; [defaults setobject:testo.text forkey:@"text"]; [defaults synchronize]; }
then try tot retrieve values in tableview :
// nsarray @synthesize dataarray; - (void)viewdidload { nsuserdefaults *prefs = [nsuserdefaults standarduserdefaults]; self.dataarray = [nsarray arraywithobjects:[prefs objectforkey:@"author"], [prefs objectforkey:@"text"],nil]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // there 1 section. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of time zone names. return [dataarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { .............. .............. nsstring *string = [dataarray objectatindex:indexpath.row]; // authors cell.textlabel.text = string; // text cell.detailtextlabel.text = string; return cell; }
i obtain result picture:
i understand problem, first of need know saving. u saving 1 string defaults or more, because if saving more recommend add files first of array , save defaults.
so nsmutablearray * myarray = [[[nsmutablearray alloc] init] autorelease]; [myarray addobject:myfirstauthor]; [myarray addobject:mysecondauthor]; [myarray addobject:mythirdauthor]; ....
same quotations....
and save arrays nsuserdefaults correctly did. on loading next time, take these arrays defaults , retrieve information there.
because in case u're saving 1 long string.
one more notice, not sure this, maybe saved defaults should type of nsdata, not sure, take on programming guide.=)
happy coding=)
Comments
Post a Comment