array data from json duplicated on tableview ios -
i'm displaying data webservice in tableview app array twice in tableview. i'm trying use endless scroll show paginated data without success
(void)viewdidload { [super viewdidload]; self.articlesarray = [[nsmutablearray alloc] init]; [self fetchdata:1]; } my function fetch data
-(void) fetchdata:(int)page { nsstring *urlstring = [nsstring stringwithformat:@"http://url?page=%d", (int)page]; nsurlrequest *request = [[nsurlrequest alloc] initwithurl:[nsurl urlwithstring:urlstring]]; nsdata *thedata = [nsurlconnection sendsynchronousrequest:request returningresponse:nil error:nil]; self.articlesarray = [nsjsonserialization jsonobjectwithdata:thedata options:nsjsonreadingmutablecontainers error:nil]; [self.tableview registernib:[uinib nibwithnibname:@"articlecell" bundle:nil] forcellreuseidentifier:@"articlecell"]; } and here's tableview methods please see did wrong
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section==0) { return [self.articlesarray count]; else return [self.articlesarray count]; } // return [self.articlesarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsinteger row = [indexpath row]; if (3 == (row % 4)) { // or 0 == if want first cell ad! static nsstring *myidentifier = @"adcell"; adviewcell *cell = (adviewcell *)[tableview dequeuereusablecellwithidentifier:myidentifier]; if ((cell == nil) || (![cell iskindofclass: adviewcell.class])) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"adcell" owner:self options:nil]; cell = [nib objectatindex:0]; cell = [[adviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:myidentifier] ; } gadbannerview *bannerview = [[gadbannerview alloc] initwithadsize:kgadadsizemediumrectangle]; bannerview.adunitid =@""; bannerview.rootviewcontroller =self; gadrequest *request = [gadrequest request]; [bannerview loadrequest:request]; [cell.contentview addsubview:bannerview]; return cell; } else{ static nsstring *simpletableidentifier = @"articlecell"; articleviewcell *cell = (articleviewcell *)[tableview dequeuereusablecellwithidentifier:simpletableidentifier ]; if ((cell == nil) || (![cell iskindofclass: articleviewcell.class])) { nsarray *nib = [[nsbundle mainbundle] loadnibnamed:@"articlecell" owner:self options:nil]; cell = [nib objectatindex:1]; cell = [[articleviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:simpletableidentifier] ; } nsdictionary * tempdictionary = [self.articlesarray objectatindex:indexpath.row]; nsstring *imageurl = [[self.articlesarray objectatindex:indexpath.row]objectforkey:@"featured_image"]; imageurl = [imageurl stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; [cell.thumbnailimageview sd_setimagewithurl:[nsurl urlwithstring:imageurl ] placeholderimage:nil options:sdwebimageretryfailed completed:^(uiimage *image, nserror *error, sdimagecachetype cachetype, nsurl *imageurl) { if (image){ // set image on here }else{ //something went wrong nslog(@"error occured : %@", [error description]); } }]; nsstring * title=[tempdictionary valueforkeypath:@"title.rendered"]; cell.titlelabel.text = title; return cell; } }
you can remove duplicate values once response json, before reload tableview remove duplicate elements array
nsorderedset *orderedset = [nsorderedset orderedsetwitharray:yourarray]; nsarray *arraywithoutduplicates = [orderedset array]; thanks
Comments
Post a Comment