简体   繁体   中英

The “index 1 beyond bounds” error after JSON parsing and UITableView populating

Here is my code (I need to parse multiple JSON files from a source on a remote server) and I need to populate UITableView with the SINGLE value of each parse:

The single JSON source (simple), at h**p://WWW.REMOTESERVERWITHJSONSOURCE.NET/A1:

{
  "id": 0001,
  "main": {
    "main1A": 100,
  },
  "main2": {
    "main2A": 200,
  },
  "url": "http...",
}

Alloc and initialize my Array:

    - (void)viewDidLoad {

        // other stuff

        arrayA = [[NSMutableArray alloc] initWithObjects:@"A1", @"A2", nil]; //each object A1, A2...An is the single JSON source I need to parse
        arrayB = [[NSMutableArray alloc] initWithObjects:@"B1", @"B2", nil];
    }

Parse Method:

- (void)LoadParse {

         main = [[NSMutableArray alloc] init];
         main2 = [[NSMutableArray alloc] init];

        for (int i=0; i < [arrayA count]; i++) {

            NSURL *url = [NSURL URLWithString:
                          [NSString stringWithFormat:
                           @"http://WWW.REMOTESERVERWITHJSONSOURCE.NET/%@",[arrayA objectAtIndex:i]]];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

            NSMutableDictionary *arrayMain = [JSON valueForKey:@"main"];
            [main addObject:[arrayMain objectForKey:@"main1A"]];
            NSMutableDictionary *arrayMain2 = [JSON valueForKey:@"main2"];
            [main2 addObject:[arrayMain2 objectForKey:@"main2A"]];

            [table reloadData];
            [table scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

            }

            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){

            }];

            [operation start];

            }
    }

The UITableView methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return [arrayA count];
}

// other methods

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

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

            cell.textLabel.font = [UIFont systemFontOfSize:12];
            cell.detailTextLabel.font = [UIFont systemFontOfSize:11];

            cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",[main objectAtIndex:indexPath.row],[main2 objectAtIndex:indexPath.row]]; // here I get error 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        return cell;
}

THE ERROR : when I alloc and initialize ONLY ONE object in arrayA it's all right: UITableView populates with ONE row and get values of main1 and main2; IF I alloc and initialize MORE THAN ONE object in arrayA, like in this example, app crashes with error signal SIGABRT : 'NSRangeException', reason: ' * -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' ... what is wrong? Please help me to find the trouble, thank you!

[ADDITION]

// Implement this for your numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    unsigned int mCount  = [main  count];
    unsigned int m2Count = [main2 count];

    return ( (mCount > m2Count) ? mCount : m2Count );
}

[ADDITION 2]

// Implement something like this within your cellForRowAtIndexPath routine
bool mainExist  = ( (indexPath.row < [main count])  ? YES : NO );
bool main2Exist = ( (indexPath.row < [main2 count]) ? YES : NO );

if(YES == mainExist && YES == main2Exist){
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",[main objectAtIndex:indexPath.row],[main2 objectAtIndex:indexPath.row]];
}else if(YES == main2Exist){
    cell.textLabel.text = [NSString stringWithFormat:@"___ - %@",[main2 objectAtIndex:indexPath.row]];
}else{ // YES == mainExist
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - ___",[main objectAtIndex:indexPath.row]];
}

Looks like (from your code) that you are using arrayA to drive your cell count routine, but using main and main2 to populate. Shouldnt you be using main and / or main2 to also generate your cell count routine output.

What looks to be happening is that arrayA has 2 elements as you are initializing that way, but because of the data, main and main2 only get filled with 1. Thus when your cell count routine returns 2, the cell generator routine is looking for main and main2 index 1, when the index only goes to 0.

This is just what I see straight from your code, if your code or the data are actually different, please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM