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 6642087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:53:19+00:00 2026-05-25T23:53:19+00:00

I create a table with two sections. Each section has four cells.I want to

  • 0

I create a table with two sections. Each section has four cells.I want to push to a new view by navigation controller when a certain cell is pressed by user.

However there are two sections. I don’t know how to distinguish the cell selected belongs to which section in

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

Please give me some tips. Thank you very much.

Here’s my TableViewController.m

#import "TableViewController.h"
#import "LondonController.h"
#import "NewYorkViewController.h"
#import "ParisViewController.h"
#import "TokyoViewController.h"

@implementation TableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    [tableDataSource release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStyleGrouped];
    [table setDataSource:self];
    [table setDelegate:self];   

    tableDataSource = [[NSMutableArray alloc]init];

NSMutableArray* sec1 = [[NSMutableArray alloc] init];
[sec1 addObject:@"London"];
[sec1 addObject:@"New York"];
[sec1 addObject:@"Paris"];
[sec1 addObject:@"Tokyo"];
[tableDataSource addObject:sec1];
[sec1 release];

NSMutableArray* sec2 = [[NSMutableArray alloc] init];
[sec2 addObject:@"Elton John"];
[sec2 addObject:@"Michael Jackson"];
[sec2 addObject:@"Little Prince"];
[sec2 addObject:@"SMAP"];
[tableDataSource addObject:sec2];
[sec2 release];

[self.view addSubview:table];

[table release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    if ( tableDataSource == nil )
        return 1;
    return [tableDataSource count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{    
    NSInteger bucketCount = -1;
    NSObject *target_section;
    if ( tableDataSource == nil )
        return 0;
    if( ( bucketCount = [tableDataSource count] ) < 1 || bucketCount <= section || (target_section = [tableDataSource objectAtIndex:section ]) == nil )
        return 0;
    return [ (NSMutableArray*)target_section count ];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:    [NSString stringWithFormat:@"Cell %i",indexPath.section]];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];
    }

    cell.textLabel.text = (NSString*)[ (NSMutableArray*)[tableDataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"City";
    }
    else if(section == 1)
    {
        return @"Person";
    }
    else 
    {
        return @"Nothing;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if (indexPath.row == 0) 
    {
        LondonViewController *londonViewController = [[LondonViewController alloc] initWithNibName:@"LondonViewController" bundle:nil];
        taipeiViewController.title = @"London Info";
        [self.navigationController pushViewController:londonViewController animated:YES];   
        [londonViewController release];
    }

    else if (indexPath.row == 1) 
    {
        NewYorkViewController *newYorkViewController = [[NewYorkViewController alloc] initWithNibName:@"NewYorkViewController" bundle:nil];
        newYorkViewController.title = @"New York Info";
        [self.navigationController pushViewController:newYorkViewController animated:YES];   
        [newYorkViewController release];
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
  • 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-05-25T23:53:19+00:00Added an answer on May 25, 2026 at 11:53 pm

    In

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

    you should check indexPath.section value to determine the section of the selected cell.

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

Sidebar

Related Questions

I have a very simple table with two columns, but has 4.5M rows. CREATE
How can we create 2 table views in a single view without using section
I'm trying to create a table with two columns comprising the primary key in
I have two tables: codes_tags and popular_tags. codes_tags CREATE TABLE `codes_tags` ( `code_id` int(11)
I have two tables: entitytype and project . Here are the create table statements:
I have two tables A and B as defined bellow. create table A (
First.. here are the two tables I've created (sans irrelevant columns).. CREATE TABLE users_history1
How can I efficiently create a unique index on two fields in a table
I am trying to create a unique constraint on two fields in a table.
I would like to create a table that has both a column for created

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.