iphone - iOS - DetailViewController not displaying multiple entries/values from JSON -
i'm creating simple ipad app detailviewcontroller. works great, when try use multiple level json, doesn't work anymore app crashes.
i retrieve , loop through data:
-(void) retrievedata{ nsurl * url = [nsurl urlwithstring:getdataurl]; nsdata * data = [nsdata datawithcontentsofurl:url]; jsonarray = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; fruitarray = [[nsmutablearray alloc] init]; //loop through jsonarray for(int =0;i < jsonarray.count;i++){ nsstring * cname = [[jsonarray objectatindex:i] valueforkey:@"cityname"]; nsstring * cfruit = [[jsonarray objectatindex:i] valueforkey:@"fruit"]; //add object fruitarray array [fruitarray addobject:[[fruits alloc]initwithcitydocumentname:cname andfruit:cfruit]]; } //reload table view [self.tableview reloaddata]; }
using json:
[{ "id": "1", "cityname": "london", "fruit":"apple" }, { "id": "2", "cityname": "berlin", "fruit":"pear" }]
and set labels(i hook them storyboard/nib):
- (void)setlabels{ citynamelabel.text = currentcity.documentname; fruitlabel.text = currentcity.documentfruit; }
all above work fine.
but create multiple values in json fruits, not work! new json:
[{ "id": "1", "cityname": "london", "fruit": ["apple", "pear", "orange", "lemon"] }, { "id": "2", "cityname": "berlin", "fruit": ["mango", "melon", "tomatoe", "avocado"] }]
it looks me need somehow programatically create labels , loop on them? basically, need see multiple values in detail view controller each id
how can it?
i need here :( thank you.
edit: able create array rather strings when i'm creating buttons them won't let me add title based on what's in array:
nsmutablearray * cfruit = [[jsonarray objectatindex:i] valueforkey:@"fruit"];
and then:
- (void)setlabels{ for(int i=0;i<=currentcity.documentfruit.count;i++){ uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect]; [button addtarget:self action:@selector(setlabels) forcontrolevents:uicontroleventtouchupinside]; //below error - looks doesn't setting titles array [button settitle:[currentcity.documentfruit objectatindex:i] forstate:uicontrolstatenormal]; button.frame = cgrectmake(20.0, buttonheightplusoffsetbetweenbuttons * , 280.0, 40.0); //set tag future identification [button settag:i]; [self.view addsubview:button]; } }
here providing complete code, so
#import "viewcontroller.h" #import "fruitsbo.h" @interface viewcontroller () @property (strong, nonatomic) iboutlet uitableview *tblfruit; @property (nonatomic, strong) nsmutablearray *arrfruitdata; @end @implementation viewcontroller @synthesize arrfruitdata; - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. arrfruitdata = [[nsmutablearray alloc] init]; [self loadlocaljson]; } -(void)loadlocaljson { nsstring *pathstringtolocalfile = [[nsbundle mainbundle] pathforresource:@"fruit" oftype:@"json"]; nserror *deserializingerror; nsurl *localfileurl = [nsurl fileurlwithpath:pathstringtolocalfile]; nsdata *contentoflocalfile = [nsdata datawithcontentsofurl:localfileurl]; nsarray *objects = [nsjsonserialization jsonobjectwithdata:contentoflocalfile options:nsjsonreadingallowfragments error:&deserializingerror]; [self parsefruit:objects]; } -(void)parsefruit:(nsarray *)arrfruits { (nsdictionary *dicttemp in arrfruits) { fruitsbo *fruit = [[fruitsbo alloc] init]; fruit.fruitid = [dicttemp objectforkey:@"id"]; fruit.cityname = [dicttemp objectforkey:@"cityname"]; fruit.arrfruit = [dicttemp objectforkey:@"fruit"]; [arrfruitdata addobject:fruit]; fruit = nil; } [self.tblfruit reloaddata]; } #pragma mark - tableview delegate - - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [self.arrfruitdata count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { fruitsbo *obj = [self.arrfruitdata objectatindex:section]; return [obj.arrfruit count]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 50; } - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section { return 30; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { fruitsbo *obj = [self.arrfruitdata objectatindex:section]; return obj.cityname; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *simpletableidentifier = @"fruitidentifier"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableidentifier]; } fruitsbo *objtemp = [self.arrfruitdata objectatindex:indexpath.section]; cell.textlabel.text = [objtemp.arrfruit objectatindex:indexpath.row]; return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { [tableview deselectrowatindexpath:indexpath animated:yes]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end
here json
[{ "id": "1", "cityname": "london", "fruit": ["apple", "pear", "orange", "lemon"] }, { "id": "2", "cityname": "berlin", "fruit": ["mango", "melon", "tomatoe", "avocado"] },{ "id": "2", "cityname": "austin", "fruit": ["mango"] }]
here output
Comments
Post a Comment