I have an app that have one tabbarcontroller that contains a navigationcontroller for each tab. Every navigationcontroller ends up at a view controller with the name DetailViewController. But if the user navigates to DetailViewController on one tab and then switches tab and navigates to DetailViewController there too, and the app crashes with the error:
2011-08-19 21:14:45.257 Kluringar[4368:ef03] 开发者_如何学运维*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 0]'
The code that pushes DetailViewController looks like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
kluringIdString = [[kluringar objectAtIndex:indexPath.row] objectForKey:@"id"];
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
The code that gets the data from the database and adds it to the array looks like this:
-(void) getAllRowsFromTableNamed {
//—-retrieve rows—-
NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM Kluringar ORDER BY rubrik"];
sqlite3_stmt *statement;
kluringar = [[NSMutableArray alloc] init];
NSMutableDictionary *kluringarDictionary = [[NSMutableDictionary alloc] init];
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *kluringId = (char *) sqlite3_column_text(statement, 2);
NSString *kluringIdStr = [[NSString alloc] initWithUTF8String: kluringId];
char *rubrik = (char *) sqlite3_column_text(statement, 3);
NSString *rubrikStr = [[NSString alloc] initWithUTF8String: rubrik];
//---add items---
[kluringarDictionary setValue:kluringIdStr forKey:@"id"];
[kluringarDictionary setValue:rubrikStr forKey:@"rubrik"];
[kluringar addObject:[kluringarDictionary copy]];
}
//—-deletes the compiled statement from memory—-
sqlite3_finalize(statement);
}
}
Here is a link to the storyboard: storyboard
Thans in advance! :)
Your trying to access an array in this line
kluringIdString = [[kluringar objectAtIndex:indexPath.row] objectForKey:@"id"];
that should have multiple elements but it doesn't. I would suggest you look through your code and see how many elements are added to the kluringar array.
精彩评论