开发者

Application crashes when searching ONE word

I'm making my UISearchBar working and i've noticed that if i type a word, just ONE, contained in more controllers (for example "a") and i select a row, i get an unexpected crash. If i search for more than a word it works, even if contained in more than 1 controller.

i'm getting a NSRangeException:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectAtIndex:]: index 9 beyond bounds [0 .. 8]'

at line

cellValue = [array objectAtIndex:indexPath.row];

of my code (i have 3 sections)

- (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];
        }


}

(the line is not the one in the IF state)

Now. I know that it goes over the array, trying to access to the 10th element. Passing the mouse over the line "array" here

cellValue = [array objectAtIndex:indexPath.row] ;

It tells me there are 9 objects on it, while there should be 45, as i declared them here, for the key @"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开发者_JS百科 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;

}

Shouldn't it see all the object for the key @"Elementi"? I'm on it from days, i can't understand how to solve, though.

Any help appreciated

*EDIT *

I noticed that

NSDictionary *dict = [listaOggetti objectAtIndex:indexPath.section];

has only 9 elements, as well as in the first array, but i have 3 dictionaries: dictDesk, dictLion, dictRest.

How can i do to select all the 45 elements in the NSDictionary?

*EDIT 2 * Thanks to pengOne, i finally filled a global array with my 45 elements,this way

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"]];

Now all i need to do it's to adapt something like this

NSDictionary *dictionary =[listaOggetti objectAtIndex:indexPath.row];
NSArray *array = [dictionary objectForKey:@"Elementi"];
cellValue = [array objectAtIndex:indexPath.row];

to the global array i've done, so it can let me search and select contents, either from tableView and SearchView.


The error tells you that you are sending this message:

[array objectAtIndex:9];

when array has 9 object, and it only responds to

[array objectAtIndex:0];
...
[array objectAtIndex:8];

The lines you need to investigate are:

    NSArray *array = [dictionary objectForKey:@"Elementi"];
    selectedRow = [array objectAtIndex:indexPath.row];

Check [dictionary objectForKey:@"Elementi"]. It appears to be an NSArray with 9 objects, when it needs to be an NSArray with at least 10 objects in order to access the object at index 10.


For your viewDidLoad method, it looks to me like listaOggetti is an NSMutableArray with 3 objects: dictDesk has 9 elements, dictLion has 30 elements, and dictRest has 6 elements.

To fix this error, you will need to change the number of rows in section 0 to be 9, the number of rows in section 1 to be 30 and the number of rows in section 2 to be 6.


- (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];
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜