0

Here there is my simple viewDidLoad method and UITableView delegates in a ViewController:

import "HomeViewController.h"
import "AFNetworking.h"
import "SVPullToRefresh.h"

- (void)viewDidLoad
    {
        [super viewDidLoad];
        [self parseJsonMethod];
        items = [[NSMutableArray alloc] init]; // UPDATED CODE
        items2 = [[NSMutableArray alloc] init]; // UPDATED CODE

        [table addPullToRefreshWithActionHandler:^{
        [self parseJsonMethod]; // <--- what's the number of NEW items after refreshing?
    }];
}

- (void)parseJsonMethod{
    // code to parse JSON file from the net ---> here I get NSMutableArray *items 

NSURLRequest *requestJSON = [NSURLRequest requestWithURL:urlJSON];
AFJSONRequestOperation *operationJSON = [AFJSONRequestOperation
                                           JSONRequestOperationWithRequest:requestJSON
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                               NSMutableArray *results = [JSON valueForKey:@"results"];

                                               // UPDATED CODE

                                               for (id obj in [results valueForKey:@"value"]) {
                                                   if (![items containsObject:obj]) {
                                                       [items addObject:obj];
                                                   }
                                               }

                                               for (id obj2 in [results valueForKey:@"value2"]) {
                                                   if (![items2 containsObject:obj2]) {
                                                       [items2 addObject:obj2];
                                                   }
                                               }

                                               // END OF UPDATED CODE

                                               [table reloadData];
                                               [table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
                                               [table.pullToRefreshView stopAnimating];
                                           }
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                               NSLog(@"%@", [error userInfo]);
                                           }];
  [operationJSON start];
  [table reloadData];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [items count]; // <--- items is the NSMutableArray parsed from JSON file on the net: HERE I HAVE THE PROBLEM, ITS ALWAYS 10!
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:
                UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

      cell.textLabel.text = [NSString stringWithFormat:@"%@",[items objectAtIndex:indexPath.row]];
      cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[items2 objectAtIndex:indexPath.row]];

    return cell;
}

As u can see I have implemented SVPullToRefresh classes to refresh data after pulling the table, and this is perfect: after pulling I can see new data but on the SAME rows (the number of rows is 10 before and after refreshing). I'd like to add NEW rows after pulling table and new parsing, and mantain the old rows with old data (every time 10 + new items + ...). There is a simple way to do that? or at least to recognize the number of new items? maybe implementing insertRowsAtIndexPaths delegate? Please help me.

UPDATE: see new edited code, I have declared previously items but there is still some problem.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
SILminore
  • 509
  • 3
  • 10
  • 28
  • print [items count] to console in parseJsonMethod{} before calling reloadData on tableView. Does it show different # of items ? – 0x8badf00d Dec 13 '12 at 13:31
  • Check this https://gist.github.com/4277574 – 0x8badf00d Dec 13 '12 at 16:20
  • Thank you 0x8badf00d, unluckly its the same thing: I cannot see precise corrispondence between value and value2 of the same array number (I have rows with text of a item and detailText of another item) and sometimes app crashes with "index beyond bounds [0 .. 9]" error... – SILminore Dec 13 '12 at 21:17

1 Answers1

1

What you're doing does add new rows if its given new rows. Your items array is the problem here. Whatever data your getting from the JSON source and however you're adding it to the items array is the issue.

If you need more help show us your JSON code and where the items array gets populated both originally and on reloads. If there aren't new rows in items they won't be in the table.

Update

On this line: items = [results valueForKey:@"value"]; // (initial number of items = 10)

I'd try something like this:

for (id obj in [results valueForKey:@"value"]) {
    if (![items containsObject:obj]) {
        [items addObject:obj];
    }
}

This will check for duplicate items and add them to the ones you already have. Now there could still be an issue if your JSON feed returns 10 of the same item and you won't see a difference.

For ultra basic just to see it working just [items addObjectsFromArray:[results valueForKey:@"value"]];

Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • Hey @Ryan, thank you, I have now edited my question: the JSON parsing is a simple code with AFNetworking class. – SILminore Dec 13 '12 at 13:35
  • Unluckly the first method (for id obj) give 0 rows and the "ultra basic" methods give always 10 rows – SILminore Dec 13 '12 at 13:48
  • That means you aren't getting new rows. – Ryan Poolos Dec 13 '12 at 13:49
  • On these lines `NSMutableArray *results = [JSON valueForKey:@"results"];` `items = [results valueForKey:@"value"]` I'm not sure what you're doing. You're sending valueForKey to an array which is indexed not keyed. So `[results valueForKey:@"value"]` is probably ALWAYS empty. I would log `NSLog(@"%@",results);` and see where you data is exactly. – Ryan Poolos Dec 13 '12 at 13:51
  • No, @Ryan, with [results valueForKey:@"value"] items its not empty (Its precisely 10), and btw the log of results is always the parsed JSON data from the net (too long to write here) – SILminore Dec 13 '12 at 13:56
  • 1
    Well if you have 10 items. And you add 10 more objects thats 20. Lol. So either `[results valueForKey:@"value"]` is not always 10 or you didn't **add** them and rather **replaced** them. Not trying to be a jerk but if its always ten and you keep adding them like this `[items addObjectsFromArray:[results valueForKey:@"value"]];` there is no way it can't increase. – Ryan Poolos Dec 13 '12 at 14:08
  • well I have tried to declare before in viewDidLoad items = [[NSMutableArray alloc] init]; and then your for (id obj) method: now the numbers of items progressively increase (example 5->7->10) but if this number become > 10 app crashes with tipically "index 10 beyond bounds [0 .. 9]" – SILminore Dec 13 '12 at 14:22
  • Ok now we're getting somewhere! woohoo! :) Items DOES need to be declared previously. Just like you showed. Then in the JSON return add items like I suggested. Then make it crash and show me where it crashed :) – Ryan Poolos Dec 13 '12 at 14:26
  • Well @ryan-poolos, after days and days I discovered the issue (it was a problem with remote JSON data), and your code has been a great starting point... thank you very much! – SILminore Jan 11 '13 at 10:15
  • Glad you were able to figure it out :) Happy Coding – Ryan Poolos Jan 11 '13 at 13:17