sinfils sinfils - 1 year ago 62
JSON Question

array data from json duplicated on tableview ios

i'm displaying data from a webservice in my tableview app but i get the array twice in the tableview.
also i'm trying to use the endless scroll to show paginated data without success

(void)viewDidLoad {
[super viewDidLoad];

self.articlesArray = [[NSMutableArray alloc] init];
[self fetchData:1];


my function to 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

self.articlesArray = [NSJSONSerialization JSONObjectWithData:theData

[self.tableView registerNib:[UINib nibWithNibName:@"ArticleCell" bundle:nil] forCellReuseIdentifier:@"ArticleCell"];

and here's my tableview methods please see what i did wrong

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section==0)
return [self.articlesArray count];
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 you want the first cell to be an 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;
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 your image over here
//something went wrong
NSLog(@"Error occured : %@", [error description]);

NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"];

cell.titleLabel.text = title;

return cell;


Answer Source

You can remove duplicate values once you get response from json, before reload tableview remove duplicate elements from array

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:YourArray];

NSArray *arrayWithoutDuplicates = [orderedSet array];


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download