0

i have a table VC in which i have pass static array of list and when we click the item in an array it open its view controller, Now i have written another view controller at index 9 but when i run the app it shows me error of * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 10 beyond bounds [0 .. 9]'. Indexing is fine but i don't know why it is coming. My code is,

     NSString *success= [successDefaults valueForKey:@"Success"];
    if ([success isEqual:@"Success"]) {
        NSString *string1 = [successDefaults stringForKey:@"name"];

        NSLog(@"Name is %@",string1);

         LeftMenuData1=[[NSArray alloc]initWithObjects:string1,@"Home",@"Show Franchiser",@"AboutUs",@"Logout",@"Contact Us",@"Wanted",@"Terms&Conditions",@"Share App",@"View Properties",@"Chat", nil];
        [self.tableView reloadData];

    }else{

        LeftMenuData1=[[NSArray alloc]initWithObjects:@"",@"Home",@"Show Franchiser",@"AboutUs",@"Login/Register",@"Contact Us",@"Wanted",@"Terms&Conditions",@"Share App",@"Chat",nil];
        [self.tableView reloadData];

    }

}

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [LeftMenuData1 count];
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];


    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];

    }
    cell.textLabel.text=[LeftMenuData1 objectAtIndex:indexPath.row];
    cell.imageView.image=[UIImage imageNamed:[LeftMenuDataImages1 objectAtIndex:indexPath.row]];


    return cell;
}

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 40;

    NSUserDefaults *successDefaults=[NSUserDefaults standardUserDefaults];
    NSString *str111=[NSString stringWithFormat:@"%@",[successDefaults valueForKey:@"currentUser"]];
    int currentLoginStatus = [str111 intValue];
    if(currentLoginStatus == 0 && indexPath.row == 0)
        height = 0;

    return height;
}

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",selectedCell.textLabel.text);

    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    NSUserDefaults *successDefaults=[NSUserDefaults standardUserDefaults];

    NSInteger row = indexPath.row;

    NSString *str=[NSString stringWithFormat:@"%@",[LeftMenuData1 objectAtIndex:indexPath.row]];


    NSLog(@"%@",str);
    if (row == 0) {

        NSString *str111=[NSString stringWithFormat:@"%@",[successDefaults valueForKey:@"currentUser"]];

        int currentLoginStatus = [str111 intValue];
        if(currentLoginStatus == 1)
        {

            UpViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"Up"];
            UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];

            [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

        } else if(currentLoginStatus == 2)
        {
            UserProfileViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"profile"];
            UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];

            [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];
        }
    }else if(row == 1){

        HomePageViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];

        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

    }else if (row == 2){

        FransFormViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"frans"];

        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];



        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];



    }else if (row == 3){
        AboutUsViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"AboutUs"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

    }else if (row == 4){

        NSUserDefaults *successDefaults=[NSUserDefaults standardUserDefaults];

        if ([[successDefaults valueForKey:@"Success"] isEqualToString:@"Success"]) {
            [successDefaults setObject:@"NO" forKey:@"Success"];
            [successDefaults setObject:@"0" forKey:@"currentUser"];

            [successDefaults synchronize];
            [successDefaults synchronize];
            [app.drawer toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];

        }else{
            GeneralLoginViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"General"];
            UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
            [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

        }

    }else if(row == 5){
        ContactInfoViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"ContactInfo"];

        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];

        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];
    }else if (row == 6){
        WantedViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"WantedViewController"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];
    }else if (row == 7){

        Terms_ConditionViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"Terms"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];
    }else if (row == 8){

        ShareViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"Share"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

    }
   else if (row==9) {
       TestChat *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Chat"];
        [self.navigationController pushViewController:vc animated:YES];
    }

    else if (row == 10){
        NSUserDefaults *successDefaults=[NSUserDefaults standardUserDefaults];

        if ([[successDefaults valueForKey:@"Success"] isEqualToString:@"Success"]){

        ViewPersonalTableViewController *presales = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewProps"];
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:presales];
        [app.drawer setCenterViewController:nav withCloseAnimation:true completion:nil];

        }


    }else{
    }

}
10
  • 3
    Post less code. Commented Oct 5, 2017 at 12:27
  • when your first portion of code executed ? Commented Oct 5, 2017 at 12:28
  • What is the number count of LeftMenuDataImages1? Commented Oct 5, 2017 at 12:34
  • my code was working fine till i haven't added index 9 after adding another option at index 9 to show TestChat view controller it starts showing me this error, When i remove TestChat vc from index 9 and gives index 9 to below VC it shows no error. @iPatel Commented Oct 5, 2017 at 12:36
  • 1
    It's happen because LeftMenuData1 has different count of items based on ` NSString *success= [successDefaults valueForKey:@"Success"]; if ([success isEqual:@"Success"])` condition manage it. Commented Oct 5, 2017 at 12:39

1 Answer 1

1

In the start of your code, you check if a string is equal to "Success". If it is, you create the array LeftMenuData1 with 11 elements, including string1 and "View Properties". If it is not "Success", then you tell that array to have 10 elements, including @""(instead of string1 but no "View Properties".

In numberOfRowsInSection you return the number of elements in LeftMenuData1 which is either 10 or 11.

In cellForRowAtIndexPath (which will be called either 10 or 11 times) you create each cell with this text: cell.textLabel.text=[LeftMenuData1 objectAtIndex:indexPath.row];. This is fine, because the LeftMenuData1 already has the same number of elements as there are indexes in your tableView. But you also do this: cell.imageView.image=[UIImage imageNamed:[LeftMenuDataImages1 objectAtIndex:indexPath.row]]; which tries to access another array (LeftMenuDataImages1), which (by your code) does not change its number of elements based on "Success". Since this is crashing, I am assuming that LeftMenuDataImages1 always has 10 elements.

If this is true, the crash is happening only when "Success" is true, because cellForRowAtIndexPath tries to get the eleventh element in LeftMenuDataImages1, which only has 10 elements.

Even if you add an eleventh element to you Images-array, they will be wrong, because the index of the last elements are wrong. You need to set LeftMenuDataImages1 to two different arrays based on "Success" in the exact same way you set LeftMenuData1 to be different based on "Success".

E.g like this:

 NSString *success= [successDefaults valueForKey:@"Success"];
if ([success isEqual:@"Success"]) {
    NSString *string1 = [successDefaults stringForKey:@"name"];

    NSLog(@"Name is %@",string1);

    LeftMenuData1=[[NSArray alloc]initWithObjects:string1,@"Home",@"Show Franchiser",@"AboutUs",@"Logout",@"Contact Us",@"Wanted",@"Terms&Conditions",@"Share App",@"View Properties",@"Chat", nil];
    LeftMenuDataImages1 =[[NSArray alloc]initWithObjects:someImageName,@"HomeImage",@"ShowFranchiserImage",@"AboutUsImage",@"LogoutImage",@"ContactUsImage",@"WantedImage",@"Terms&ConditionsImage",@"ShareAppImage",@"ViewPropertiesImage",@"ChatImage", nil];
    [self.tableView reloadData];

}else{

    LeftMenuData1=[[NSArray alloc]initWithObjects:@"",@"Home",@"Show Franchiser",@"AboutUs",@"Login/Register",@"Contact Us",@"Wanted",@"Terms&Conditions",@"Share App",@"Chat",nil];
    LeftMenuDataImages1=[[NSArray alloc]initWithObjects:@"EmptyImage",@"HomeImage",@"ShowFranchiserImage",@"AboutUsImage",@"Login/RegisterImage",@"ContactUsImage",@"WantedImage",@"Terms&ConditionsImage",@"ShareAppImage",@"ChatImage",nil];
    [self.tableView reloadData];

}

Just insert your own image names.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.