Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:23:32+00:00 2026-06-12T14:23:32+00:00

I have two sections, A-M and N-Z. I noticed that if I have the

  • 0

I have two sections, A-M and N-Z.

I noticed that if I have the same number of cities in each section, there won’t be a problem. However, the program will crash if I don’t have the same number of cities.

The error is

-[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 0]’

This is my code when the error generates:

- (void)viewDidLoad
{
        [super viewDidLoad];

        self.title = @"Region";

        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

        AM  = [[NSMutableArray alloc] init];
        NZ  = [[NSMutableArray alloc] init];

        [AM    addObject: @"Bologna"];
        [AM    addObject: @"Florence"];
        [AM    addObject: @"Milan"];
        [NZ    addObject: @"Naples"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return [AM count];
            break;

        case 1:
            return [NZ count];
            break;

        default:
            return section;
            break;
    }
}

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        // Configure the cell...

        NSInteger section = [indexPath section];

        switch (section) {
            case 0:
                [cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
                break;

            case 1:
                [cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
                break;

            default:
                break;
        }    
        return cell;
    }


    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        switch (section) {
            case 0:
                return @"A-M";
                break;

            case 1:
                return @"N-Z";
                break;

            default:
                break;
        }
        return nil;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString * AMPath    = self.luzonRegion      [indexPath.row];
        NSString * NZPath    = self.visayasRegion    [indexPath.row];

        switch (indexPath.section) {
            case 0:
                cityController.title = luzonRegionPath;
                NSLog(@"Selected city: %@", AMPath);
                break;

            case 1:
                cityController.title = visayasRegionPath;
                NSLog(@"Selected city: %@", NZPath);
                break;

            default:
                break;
        }

        [[self navigationController] pushViewController:cityController animated:YES];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:23:33+00:00Added an answer on June 12, 2026 at 2:23 pm

    you try like this,

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.title = @"Region";
    
        tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,480) style:UITableViewStyleGrouped] autorelease];
        tableView.delegate=self;
        tableView.dataSource=self;
        [self.view addSubview:tableView];
        AM  = [[NSMutableArray alloc] init];
        NZ  = [[NSMutableArray alloc] init];
    
        [AM    addObject: @"Bologna"];
        [AM    addObject: @"Florence"];
        [AM    addObject: @"Milan"];
        [NZ    addObject: @"Naples"];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 2;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        switch (section) {
            case 0:
                return [AM count];
                break;
    
            case 1:
                return [NZ count];
                break;
    
            default:
                return section;
                break;
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        // Configure the cell...
    
        NSInteger section = [indexPath section];
    
        switch (section) {
            case 0:
                [cell.textLabel setText: [AM objectAtIndex: [indexPath row]]];
                break;
    
            case 1:
                [cell.textLabel setText: [NZ objectAtIndex: [indexPath row]]];
                break;
    
            default:
                break;
        }    
        return cell;
    }
    
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        switch (section) {
            case 0:
                return @"A-M";
                break;
    
            case 1:
                return @"N-Z";
                break;
    
            default:
                break;
        }
        return nil;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      //  NSString * AMPath    = self.luzonRegion      [indexPath.row];
      //  NSString * NZPath    = self.visayasRegion    [indexPath.row];
    
        switch (indexPath.section) {
            case 0:
                //cityController.title = luzonRegionPath;
                NSLog(@"Selected city: %@", [AM objectAtIndex: [indexPath row]]);
                break;
    
            case 1:
                //cityController.title = visayasRegionPath;
                NSLog(@"Selected city: %@",[NZ objectAtIndex: [indexPath row]]);
                break;
    
            default:
                break;
        }
    
       // [[self navigationController] pushViewController:cityController animated:YES];
    }
    

    it is working fine…..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have two images with transparent sections that each have a map and separately
I have a UITableView with two sections: free and paid. Each section has a
I'm creating a WP theme that will have two sections midway down the page.
I have noticed that a problem in my app is caused because the Datasource
I have a TableViewController that is using a grouped Style and has two(2) sections.
This problem has kept me busy for the last hours. I have two sections
I have a UITableViewController which has two sections. The first section shows a single
I have a UITableView that implements UISearchBarDelegate. I have two sections in my self.searchDisplayController.searchResultsTableView
I have a UITableView With Two Sections. First section has one row and the
I have two sections on my PHP form. One section allows to enter Customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.