ios - Dynamic number of Sections & Row in TableView -



ios - Dynamic number of Sections & Row in TableView -

i beginner in objective-c , have lot of problem managing tables ! in case, have table variable number of sections, , each section, have variable number of rows.

in method "gettimeslots" json represents list of appointments. here little piece of json:

data{ "sunday, august 10, 2014" = { timeslots = ( { id = 1013; idpatient = "<null>" status = 0; timeend = 5:15 p.m.; timestart = 5:00 p.m; } { id = 1045; idpatient = "<null>" status = 0; timeend = 5:30 p.m.; timestart = 5:15 p.m.; } ); }; "sunday, july 13, 2014" = { timeslots = ( { id = 705; idpatient = "<null>" status = 0; timeend = 4:00 p.m; timestart = 3:45 p.m.; } { id = 725; idpatient = "<null>" status = 0; timeend = 4:15 p.m.; timestart = 4:00 p.m; } { id = 745; idpatient = "<null>" status = 0; timeend = 4:30 p.m timestart = 4:15 p.m.; } { id = 1009; idpatient = "<null>" status = 0; timeend = 5:15 p.m.; timestart = 5:00 p.m; } { id = 1041; idpatient = "<null>" status = 0; timeend = 5:30 p.m.; timestart = 5:15 p.m.; } ); } , lot of dynamics days , dynamics appointments.... };

i find how add together in table "tabletimeslots" 1 section per day don't find how add together appointments associated these days. tried many things nil done :(

this code :

@interface agendaviewcontroller () { nsmutablearray * sectionsarray; nsmutablearray * rowsinsectionsarray; } @end @implementation agendaviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } homecoming self; } - (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. // title self.title = @"agenda"; sectionsarray = [[nsmutablearray alloc]init]; rowsinsectionsarray = [[nsmutablearray alloc]init]; [self gettimeslots]; [[self tabletimeslots]setdelegate:self]; [[self tabletimeslots]setdatasource:self]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming [sectionsarray count]; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming [rowsinsectionsarray count]; } -(nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { homecoming [sectionsarray objectatindex:section]; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if(!cell) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.textlabel.text = [rowsinsectionsarray objectatindex:indexpath.row]; homecoming cell; } - (void)gettimeslots { nsurl *url = [nsurl urlwithstring:getdataurl]; nsdata *alldata = [nsdata datawithcontentsofurl:url]; nserror *error; id jsonobject = [nsjsonserialization jsonobjectwithdata:alldata options:nsjsonreadingmutablecontainers error:&error]; nsdictionary *data = [jsonobject objectforkey:@"data"]; (int = 0; < data.count; i++) { nsarray *days = [data allkeys]; [sectionsarray addobject:days[i]]; nsarray *tsarray = [data objectforkey:[days objectatindex:i]]; nsarray *tslist = [tsarray valueforkey:@"timeslots"]; (nsdictionary * onets in tslist) { nsstring *tsid = [onets objectforkey:@"id"]; nsstring *tsstatus = [onets objectforkey:@"status"]; nsstring *tsstart = [onets objectforkey:@"timestart"]; nsstring *tsend = [onets objectforkey:@"timeend"]; nsstring *tsidp = [onets objectforkey:@"idpatient"];

for rows, seek not work :

[rowsinsectionsarray addobject:tsid]; nsarray *newrow = [nsarray arraywithobject:[nsindexpath indexpathforrow:[rowsinsectionsarray count]-1 insection:i]]; [[self tabletimeslots] insertrowsatindexpaths:newrow withrowanimation:uitableviewrowanimationtop]; } } [self.tabletimeslots reloaddata]; } @end

if help me great because i'm lost ! in advance

your tableview builds sections , rows datasource. datasource says tableview should have [sectionsarray count] rows (that works) , [rowsinsectionsarray count] rows.

in gettimeslots add together timeslots same array. way tableview thinks sections should have same number of rows, isn't case. should have 1 array of rows per section, best within array:

rowsinsectionsarray = [[nsmutablearray alloc] init]; ... nsmutablearray *timeslotsinday = [[nsmutablearray alloc] init]; (nsdictionary * onets in tslist) { nsstring *tsid = [onets objectforkey:@"id"]; [timeslotsinday addobject:tsid]; } [rowsinsectionsarray addobject:timeslotsinday];

then in -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section function do

if (rowsinsectionsarray.count > section) { homecoming rowsinsectionsarray[section].count; }

ios 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 -