Wednesday, 15 May 2013

ios - How to form dynamic rows and sections in UITableView based on response from webservice -



ios - How to form dynamic rows and sections in UITableView based on response from webservice -

i able fetch response web services,which storing in array.this array has separated sections , rows of uitableview.in response webservice announcements having same computeddate should section title , proclamation title has rows of section based on computeddate.i able form haeders issue rows mutual sections. o/p in uitableview should be:

june 21,2014 performance , load june 20,2014 teest sample june 19,2014 samp june 18,2014 qwerty

the response format is:

{ "resultset1": [ { "announcementid": 3, "announcementtitle": "performance , load", "announcementpostedtime": "21 jun,2014,11:07 am", "computeddate": "jun 21,2014", },{ "announcementid": 22, "announcementtitle": "my teest", "announcementpostedtime": "20 jun,2014,10:11 am", "computeddate": "jun 20,2014", }, { "announcementid": 21, "announcementtitle": "sample", "announcementpostedtime": "20 jun,2014,10:11 am", "computeddate": "jun 20,2014", }, { "announcementid": 20, "announcementtitle": "all samp", "announcementpostedtime": "19 jun,2014,10:11 am", "computeddate": "jun 19,2014", }, { "announcementid": 19, "announcementtitle": "qwerty", "announcementpostedtime": "18 jun,2014,10:10 am", "computeddate": "jun 18,2014", } ] }

my code isbelow:

arrresult=[dict valueforkey:@"resultset1"]; listofobjects = [nsmutablearray array]; (nsdictionary *dictressub in arrresult) { announcements *at = [[announcements alloc] init]; at.announcementtitle = [dictressub valueforkey:@"announcementtitle"]; at.announcementpostedtime = [dictressub valueforkey:@"announcementpostedtime"]; at.computeddate=[dictressub valueforkey:@"computeddate"]; [listofobjects addobject:at]; } int i; (i=0 ;i< [listofobjects count];i++){ announcements *atannouncement1p1; nsstring *str1p1; announcements *atannouncement1=[listofobjects objectatindex:i]; nsstring *str1=[nsstring stringwithformat:@"%@",atannouncement1.computeddate]; if (i==0) { [arrheaders addobject:str1]; [arrfirstobj addobject:atannouncement1]; } if (i<[listofobjects count]-1) { atannouncement1p1=[listofobjects objectatindex:i+1]; str1p1=[nsstring stringwithformat:@"%@",atannouncement1p1.computeddate]; if ([str1 isequaltostring:str1p1]) { nslog(@"already there"); [arrfirstobj addobject:atannouncement1p1]; } else{ [arrheaders addobject:str1p1]; } } } } // [tablevwcategories reloaddata]; tablevwcategories.frame=cgrectmake(0, 70, 320, 300); } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview{ homecoming [arrheaders count]; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section{ homecoming [arrheaders objectatindex:section]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ homecoming [arrfirstobj count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ static nsstring *identifier=@"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:identifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@"identifier"]; } announcements *at = [arrfirstobj objectatindex:indexpath.row]; cell.textlabel.text= [nsstring stringwithformat:@"%@",at.announcementtitle]; cell.selectionstyle=uitableviewcellselectionstylenone; cell.backgroundcolor=[uicolor clearcolor]; homecoming cell; }

the problem current code isn't clear me don't know doesn't happen should happen or vice versa. i'm still guessing bit. see not implementing viewforheaderinsection utilize implement custom headers. think need heightforheaderinsection set different 0 (obviously).

-(uiview *)tableview:(uitableview *)tableview viewforheaderinsection:(nsinteger)section { // simple on fly generated uiview, homecoming own subclass of uiview uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, tableview.frame.size.width, 18)]; uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(10, 5, tableview.frame.size.width, 18)]; [label setfont:[uifont boldsystemfontofsize:12]]; label.text = [arrheaders objectatindex:section]; homecoming view; } - (cgfloat)tableview:(uitableview *)tableview heightforheaderinsection:(nsinteger)section; { // should same uiview homecoming in previous function homecoming 18.0f; }

ios objective-c uitableview

No comments:

Post a Comment