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]'