我正在让我的UISearchBar工作,我注意到如果我输入一个单词,只包含一个,包含在更多控制器中(例如“a”)并且我选择一行,我会遇到意外崩溃。如果我搜索的不止一个单词就行了,即使它包含在一个以上的控制器中也是如此。
我收到NSRangeException:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 9 beyond bounds [0 .. 8]'
在第
行cellValue = [array objectAtIndex:indexPath.row];
我的代码(我有3个部分)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//Get the nib
NSString *cellValue = nil;
if(searching)
cellValue = [copyListOfItems objectAtIndex:indexPath.row];
else {
NSDictionary *dictionary =[listaOggetti objectAtIndex:indexPath.row];
NSArray *array = [dictionary objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row];
}
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
NSArray *array = [dict objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row] ; //here's the error
if (indexPath.section == 0)
{
if ([cellValue isEqual: @"FirstNib"]){
FIrstViewController *firstController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.navigationController pushViewController:appsController animated:YES];
[firstController release];
if ([cellValue isEqual: @"SecondNib"]){
secondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
[self.navigationController pushViewController:secondController animated:YES];
[secondController release];
}
//and so on
}
if (indexPath.section == 1){
if ([cellValue isEqual: @"ThirdNib"]){
ThirdViewController *firstController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self.navigationController pushViewController:ThirdController animated:YES];
[ThirdController release];
}
//and so on
}
if (indexPath.section == 2) {
if ([cellValue isEqual @"FourthNib"]){
FourthViewController *firstController = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil];
[self.navigationController pushViewController:FourthController animated:YES];
[FourthController release];
}
}
(该行不是IF状态的行)
现在。我知道它遍及数组,试图访问第10个元素。 将鼠标移到这里的“数组”行
cellValue = [array objectAtIndex:indexPath.row] ;
它告诉我它上面有9个物体,而我应该在这里宣布它们为45个钥匙@“Elementi”:
- (void)viewDidLoad {
[super viewDidLoad];
listaOggetti = [[NSMutableArray alloc] init];
NSArray *arrayDesk = [NSArray arrayWithObjects: @"First",@"Second",@"Third"..@"9th", nil];
NSArray *sortedDesk = [arrayDesk sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictDesk = [NSDictionary dictionaryWithObject:sortedDesk forKey:@"Elementi"];
NSArray *arrayLion = [NSArray arrayWithObjects:@"First1",@"Second1" ... @"30th", nil];
NSArray *sortedLion = [arrayLion sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictLion = [NSDictionary dictionaryWithObject:sortedLion forKey:@"Elementi"];
NSArray *arrayRestore = [NSArray arrayWithObjects:@"First2",@"Second2" ... @"6th", nil];
NSArray *sortedRestore = [arrayRestore sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSDictionary *dictRest = [NSDictionary dictionaryWithObject:sortedRestore forKey:@"Elementi"];
[listaOggetti addObject:dictDesk];
[listaOggetti addObject:dictLion];
[listaOggetti addObject:dictRest];
copyListOfItems = [[NSMutableArray alloc] init];
self.navigationItem.title = NSLocalizedString(@"TIPS", @"Tips");
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"window.png"]];
//Add the search bar
self.tableView.tableHeaderView = searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
searching = NO;
letUserSelectRow = YES;
CGRect bounds = self.tableView.bounds;
bounds.origin.y = bounds.origin.y + searchBar.bounds.size.height;
self.tableView.bounds = bounds;
}
它不应该看到键@"Elementi"
的所有对象吗?我从几天开始,我无法理解如何解决。
任何帮助表示赞赏
* 编辑*
我注意到了
NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];
只有9个元素,以及第一个数组,但我有3个词典:dictDesk,dictLion,dictRest。
如何选择NSDictionary中的所有45个元素?
* 编辑2 * 感谢pengOne,我终于用我的45个元素填充了一个全局数组,这样
NSMutableArray *allObjects = [[NSMutableArray alloc] init];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:0] objectForKey:@"Elementi"]];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:1] objectForKey:@"Elementi"]];
[allObjects addObjectsFromArray:[[listaOggetti objectAtIndex:2] objectForKey:@"Elementi"]];
现在我需要做的就是适应这样的事情
NSDictionary *dictionary =[listaOggetti objectAtIndex:indexPath.row];
NSArray *array = [dictionary objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row];
到我已经完成的全局数组,所以它可以让我从tableView和SearchView中搜索和选择内容。
答案 0 :(得分:2)
错误告诉您发送此消息:
[array objectAtIndex:9];
当array
有9个对象时,它只响应
[array objectAtIndex:0];
...
[array objectAtIndex:8];
您需要调查的行是:
NSArray *array = [dictionary objectForKey:@"Elementi"];
selectedRow = [array objectAtIndex:indexPath.row];
检查[dictionary objectForKey:@"Elementi"]
。它似乎是一个带有9个对象的NSArray
,当它需要是一个NSArray
,至少有10个对象才能访问索引为10的对象。
对于您的viewDidLoad
方法,我认为listaOggetti
是NSMutableArray
,包含3个对象:dictDesk
有9个元素,dictLion
有30个元素,dictRest
有6个元素。
要修复此错误,您需要将第0部分中的行数更改为9,将第1部分中的行数更改为30,将第2部分中的行数更改为6。
答案 1 :(得分:1)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* cellValue=@"";
if (searching)
cellValue = [copyListOfItems objectAtIndex:indexPath.row];
else{
NSDictionary *dictionary = [listaOggetti objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row];
}
if ([cellValue isEqual: @"Apps on specific Desks"] || [cellValue isEqual: @"Applicazioni su più Scrivanie"]){
AppsViewController *appsController = [[AppsViewController alloc] initWithNibName:@"AppsViewController" bundle:nil];
[self.navigationController pushViewController:appsController animated:YES];
[appsController release];
}
}