ios - UITableView not properly populating sections with NSArray contents -



ios - UITableView not properly populating sections with NSArray contents -

my parse cloud code sends json ios app next structure:

what want iterate through , create new section in uitableview every object in matchcenterarray.

in instance, there 3 objects in array, each contains top 3 nsdictionary value array of 3 items, each of yet array of properties. can see, want set each section has 3 cells, 1 each of top 3 items of respective matchcenterarray object. want pull properties of each item , display in each cell texlabel, detailtextlabel, , thumbnail.

i've tried using loop solution, displays same item in cells of array. because i'm looping through matchcenterarray objects, not additionally looping through items in objects, can seen here:

cell.textlabel.text = [[[[_matchcenterarray objectatindex:i] objectforkey:@"top 3"] objectatindex:0]objectforkey:@"title"];

what thinking of doing nesting loop within one, in place of objectatindex:0, sending message loop doesn't work.

matchcenterviewcontroller.m:

#import "matchcenterviewcontroller.h" #import <uikit/uikit.h> @interface matchcenterviewcontroller () <uitableviewdatasource, uitableviewdelegate> @property (nonatomic, strong) uitableview *matchcenter; @end @implementation matchcenterviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { } homecoming self; } - (void)viewdidload { [super viewdidload]; self.matchcenter = [[uitableview alloc] initwithframe:self.view.bounds style:uitableviewcellstylesubtitle]; self.matchcenter.frame = cgrectmake(0,50,320,self.view.frame.size.height-100); _matchcenter.datasource = self; _matchcenter.delegate = self; [self.view addsubview:self.matchcenter]; _matchcenterarray = [[nsarray alloc] init]; } - (void)viewdidappear:(bool)animated { self.matchcenterarray = [[nsarray alloc] init]; [pfcloud callfunctioninbackground:@"matchcentertest" withparameters:@{ @"test": @"hi", } block:^(nsarray *result, nserror *error) { if (!error) { _matchcenterarray = result; [_matchcenter reloaddata]; nslog(@"result: '%@'", result); } }]; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming _matchcenterarray.count; } //the part setup sections , deleting of said sections - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section { homecoming 21.0f; } - (uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section { uiview *headerview = [[uiview alloc] initwithframe:cgrectmake(0, 0, 320, 21)]; headerview.backgroundcolor = [uicolor lightgraycolor]; // _searchterm = [[self.matchcenterarray firstobject] objectforkey:@"search term"]; uilabel *headerlabel = [[uilabel alloc] initwithframe:cgrectmake(8, 0, 250, 21)]; // headerlabel.text = [nsstring stringwithformat:@"%@", searchterm]; // headerlabel.font = [uifont boldsystemfontofsize:[uifont systemfontsize]]; // headerlabel.textcolor = [uicolor whitecolor]; headerlabel.backgroundcolor = [uicolor lightgraycolor]; [headerview addsubview:headerlabel]; uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; button.tag = section + 1000; button.frame = cgrectmake(300, 2, 17, 17); [button setimage:[uiimage imagenamed:@"xbutton.png"] forstate:uicontrolstatenormal]; [button addtarget:self action:@selector(deletebuttonpressed:) forcontrolevents:uicontroleventtouchupinside]; [headerview addsubview:button]; homecoming headerview; } - (ibaction)deletebuttonpressed:(uibutton *)sender { nslog(@"search term: '%@'", _searchterm); [pfcloud callfunctioninbackground:@"deletefrommatchcenter" withparameters:@{ @"searchterm": _searchterm, } block:^(nsdictionary *result, nserror *error) { if (!error) { nslog(@"result: '%@'", result); } }]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming 3; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { // initialize cell static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { // if no cell dequeued create new 1 cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } (int = 0; i<[_matchcenterarray count]; i++) { // populate dictionary results //nsdictionary *matchcenterdictionary= [_matchcenterarray objectatindex:indexpath.row]; // title of item cell.textlabel.text = [[[[_matchcenterarray objectatindex:i] objectforkey:@"top 3"] objectatindex:0]objectforkey:@"title"]; cell.textlabel.font = [uifont boldsystemfontofsize:12]; // cost of item cell.detailtextlabel.text = [nsstring stringwithformat:@"$%@", [[[[_matchcenterarray objectatindex:i] objectforkey:@"top 3"] objectatindex:0]objectforkey:@"price"]]; cell.detailtextlabel.textcolor = [uicolor colorwithred:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f]; // image of item nsdata *imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:[[[[_matchcenterarray objectatindex:i] objectforkey:@"top 3"] objectatindex:0] objectforkey:@"image url"]]]; [[cell imageview] setimage:[uiimage imagewithdata:imagedata]]; //imageview.frame = cgrectmake(45.0,10.0,10,10); } homecoming cell; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } /* #pragma mark - navigation - (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // new view controller using [segue destinationviewcontroller]. // pass selected object new view controller. } */ @end

forget loops, delegate method run on loop iteration number equal datasource count.

numberofsectionsintableview

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming _matchcenterarray.count; }

numberofrowsinsection

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ homecoming _matchcenterarray[section][@"top 3"].count; }

cellforrowatindexpath

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ // initialize cell static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { // if no cell dequeued create new 1 cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } // title of item cell.textlabel.text = _matchcenterarray[indexpath.section][@"top 3"][indexpath.row][@"title"]; cell.textlabel.font = [uifont boldsystemfontofsize:12]; // cost of item cell.detailtextlabel.text = [nsstring stringwithformat:@"$%@", _matchcenterarray[indexpath.section][@"top 3"][indexpath.row][@"price"]; cell.detailtextlabel.textcolor = [uicolor colorwithred:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f]; // image of item nsdata *imagedata = [nsdata datawithcontentsofurl:[nsurl urlwithstring:_matchcenterarray[indexpath.section][@"top 3"][indexpath.row][@"image url"]]]; [[cell imageview] setimage:[uiimage imagewithdata:imagedata]]; homecoming cell; } don't harcode array count, if json changes, code need change. in future should loading images asynchronously using library sdwebimage in order avoid lags.

ios objective-c arrays json uitableview

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -