0

My table keeps crashing. "Index 1 beyond bounds". I return the an array count for the number of rows in the section, but as soon as I scroll to the second row (index 1) the app crashes.

I have two sections. One with a set number of rows and the second section is a dynamically created based upon the number of items in the array.

Any thoughts?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return 2; //two seperate areas
}

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

        if (section == 0) {
            return 16; 
        } else {
            NSLog(@"Section: %ld, NumberOfRows: %lu", (long)section, (unsigned long)[self.arrImages count]);
            return [self.arrImages count]; 
        }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];


        switch (section) {
            case 0:

                 cell =  [super tableView:tableView  cellForRowAtIndexPath:indexPath];
                break;

            case 1:

                cell = [tableView dequeueReusableCellWithIdentifier:@"cellPhoto"];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellPhoto"];
                }
                NSLog(@"Row: %lu\n%@", (unsigned long)row, [[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"]);

                cell.imageView.image = [self imageFromURL:[[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"]  fromCache:YES];
                NSLog(@"completed");
                break;
        }


    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = [indexPath section];

    [self doneData];

        switch (section) {
            case 1:
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
                [self performSegueWithIdentifier:@"segue_showphoto" sender:self];
                break;
       }
}

Here is my crash log

2015-02-12 19:32:51.817 Tops[17236:5681531] Row: 0
https://somethinghere.jpeg
2015-02-12 19:32:51.825 Tops[17236:5681531] completed
2015-02-12 19:32:51.855 Tops[17236:5681531] Row: 1
https://somthinghere.jpeg
2015-02-12 19:32:51.856 Tops[17236:5681531] completed
2015-02-12 19:32:51.862 Tops[17236:5681531] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • 1
    I think you should set an [exception breakpoint](http://stackoverflow.com/a/17802723/74815) and see which array you're accessing out of bounds, and what its contents are, and why you haven't filled it yet. – i_am_jorf Feb 13 '15 at 00:44
  • Set an exception breakpoint. I don't think your app is crashing in this code. – Paulw11 Feb 13 '15 at 00:45
  • 1
    When I hear about a problem like this, I go straight to `numberOfRowsInSection` and confirm that it returns the count of some array, then I look at `cellForRowAtIndexPath` and make sure that the code dereferences the same array in the same way. Your code is 50% good on that test (above average, by the way :-)). `cellForRowForIndexPath` uses the super implementation for section 0, then so should `numberOfRowsInSection`. I'd give this as the answer, but I agree with the other commenters: we're not certain where the problem is. – danh Feb 13 '15 at 00:46
  • What's driving me mad is: If i return 1 for numberOfRowsInSection then everything is fine. I'll break point it all out and see what I come up with. – user-44651 Feb 13 '15 at 00:51
  • Learn how to get the exception stack trace. – Hot Licks Feb 13 '15 at 01:15
  • Any suggestions on how to start with that? – user-44651 Feb 13 '15 at 01:18
  • Go to the breakpoint navigator and add an exception breakpoint. Then run your app and see which line causes the exception – Paulw11 Feb 13 '15 at 01:26

1 Answers1

2

The implementation I was missing was:

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 0; 
}
user-44651
  • 3,924
  • 6
  • 41
  • 87