Friday 15 August 2014

objective c - Load More JSON Data in Tableview with Pagination in iOS -



objective c - Load More JSON Data in Tableview with Pagination in iOS -

i want create application shows json info in uitableview in ios.here webservices contain 3 4 page.so,i want when table view scrolled load next page data. code it

- (void)viewdidload { pagenum=1; nsurl * url=[nsurl urlwithstring:[nsstring stringwithformat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]]; [self.newstable setshowshorizontalscrollindicator:no]; [self.newstable setshowsverticalscrollindicator:no]; [super viewdidload]; dispatch_async(kbgqueue, ^{ info = [nsdata datawithcontentsofurl: url]; [self performselectoronmainthread:@selector(fetcheddata:) withobject:data waituntildone:yes]; }); } -(void)fetcheddata:(nsdata *)responsedata { nserror* error; self.json = [nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&error]; self.dataarray=[_json objectforkey:@"data"]; if (self.dataarray.count > 0) { dispatch_async(dispatch_get_main_queue(), ^{ [self.newstable reloaddata]; }); } nslog(@"images,%@",self.dataarray); } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming self.dataarray.count; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { homecoming 1; } -(tablecell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier=@"cell"; tablecell *cell=[tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell ==nil) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"tablecell" owner:self options:nil]; cell = [nib objectatindex:0]; } { nsdictionary *dict = [self.dataarray objectatindex:indexpath.section]; nsstring *img2=[dict valueforkey:@"post_image"]; [cell.newsimage sd_setimagewithurl:[nsurl urlwithstring:img2] placeholderimage:[uiimage imagenamed:@"hisoka.jpg"]]; nsstring *title=[dict valueforkey:@"post_title"]; cell.headlabel.text=title; nsstring *content=[dict valueforkey:@"post_content"]; cell.descriplabel.text=content; nsdateformatter * dateformatter = [[nsdateformatter alloc]init]; [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; nsstring *date=[dict valueforkey:@"post_date"]; nsdate * datenotformatted = [dateformatter datefromstring:date]; [dateformatter setdateformat:@"d-mmm-yyyy"]; nsstring * dateformatted = [dateformatter stringfromdate:datenotformatted]; cell.datelabel.text=dateformatted; } homecoming cell; } -(void)scrollviewdidscroll:(uiscrollview *)scrollview { pagenum=pagenum+1; [self getdata]; } -(void)getdata { nsurl * url=[nsurl urlwithstring:[nsstring stringwithformat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pagenum]]; dispatch_async(kbgqueue, ^{ info = [nsdata datawithcontentsofurl:url]; [self performselectoronmainthread:@selector(fetcheddata:) withobject:data waituntildone:yes]; }); } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { nsdictionary *dictionary=[self.dataarray objectatindex:indexpath.section]; nsstring *url=[dictionary valueforkey:@"link"]; [[uiapplication sharedapplication]openurl:[nsurl urlwithstring:url]]; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { homecoming 80; }

then problem when scroll table view shows next page info first page info removed want maintain page info in table view please give me solution know question asked in past not working me please give me solution.

the problem replacing old page new page get,so need append new info the old array data.

if have allocated self.dataarray before using in fetcheddata use

nsarray* newarray=[_json objectforkey:@"data"]; if(newarray && [newarray iskindofclass:[nsarray class]]) [self.dataarray addobjectsfromarray:newarray];

else need allocate array first page get, , append next pages info later.

nsarray* newarray=[_json objectforkey:@"data"]; if(newarray && [newarray iskindofclass:[nsarray class]]){ if (!self.dataarray) self.dataarray=[nsmutablearray arraywitharray:newarray]; else [self.dataarray addobjectsfromarray:newarray]; }

ios objective-c json uitableview

No comments:

Post a Comment