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

  • Home
  • SEARCH
  • 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 9183325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:46:29+00:00 2026-06-17T18:46:29+00:00

So this is based off of my question here objective c when to use

  • 0

So this is based off of my question here

objective c when to use NSDictionary instead of NSArray

Apologies for the noob questions. I just started learning objective c and I’m really confused.

- (void)viewDidLoad
{
    [super viewDidLoad];
    headers = [NSArray arrayWithObjects:@"Header Section 1 (H1)", @"Header Section 2 (H2)", nil];
    events = [NSArray arrayWithObjects:@"H1 Content 1", @"H1 Content 2", nil];
    myInfo = [NSArray arrayWithObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];

    menuDetails = [[NSMutableDictionary alloc] init];
    for(NSString *event in events){
        [menuDetails setValue:event forKey:@"Header Section 1 (H1)"];
    }

    for(NSString *info in myInfo){
        [menuDetails setValue:info forKey:@"Header Section 1 (H2)"];
    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[menuDetails allKeys] count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]objectAtIndex:section];
}
  1. Wanted to limit this for the First Section but not on the 2nd section

    -(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section {
        return 5;
    }
    
  2. So I’m not really sure how to make this dynamic when I try to fill up the cells with this method. I’m not really sure how to make sure it selects the “Key” of the section the specific cell is at (blocked line) :


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

        MenuOptionTableCell *cell = (MenuOptionTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OptionCellIdentifier" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }

        NSDictionary *menuItem = [[menuDetails valueForKey:[[[menuDetails allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

        cell.optionName.text = [menuItem objectForKey:@"Events"];

        return cell;
    }

EDIT: GAAAHHH, the formatting is killing me. It won’t get inside the code markup

This is where Idk what to do:

cell.optionName.text = [menuItem objectForKey:@”Events”];

So what I wanted to happen is:

  • Section Header 1
    1. H1 Content 1
    2. H1 Content 2
  • Section Header 1
    1. H2 Content 1
    2. H2 Content 2
    3. H1 Content 3
  • 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-17T18:46:31+00:00Added an answer on June 17, 2026 at 6:46 pm

    I would do this by first creating a “MyMenuSection” object as follows;

    @interface MyMenuSection : NSObject {
        NSString *title;
        NSMutableArray *rows;
    }
    
    @property (nonatomic, retain) NSString *title;
    @property (nonatomic, retain) NSMutableArray *rows;
    
    @end
    
    @implementation
    
    @synthesize title;
    @synthesize rows;
    
    @dealloc {
        [title release];
        [rows release];
        [super dealloc];
    }
    
    @end
    

    Then in your viewDidLoad (or wherever you decide to inialize/assign the data) you would do this;

    -(void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSMutableArray menuDetails = [NSMutableArray array];
    
        MyMenuSection section = [[MyMenuSection alloc] init];
        section.title = @"Header Section 1 (H1)";
        [section.rows addObjects:@"H1 Content 1", @"H1 Content 2", nil];
        [menuDetails addObject:section];
        [section release];
    
        section = [[MyMenuSection alloc] init];
        section.title = @"Header Section 2 (H2)";
        [section.rows addObjects:@"H2 Content 1", @"H2 Content 2", @"H2 Content 3", nil];
        [menuDetails addObject:section];
        [section release];
    
        self.menuDetails = menuDetails;
        [menuDetails release];
    
    }
    

    Then your UITableView data and delegate callbacks would look like this;

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return [menuDetails count];
    }
    
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section == 0) {
            return 2;
        } else {
            return [[(MyMenuSection *)[menuDetails objectAtIndex:indexPath.section] rows] count];
        }
    }
    
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [(MyMenuSection *)[menuDetails objectAtIndex:section] title];
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
    
        static NSString *cellIdentifier = @"OptionCellIdentifier";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if(cell == nil){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OptionCellIdentifier" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
    
        cell.optionName.text = (NSString *)[[(MyMenuSection *)[menuDetails objectAtIndex:indexPath.section] rows] objectAtIndex:[indexPath.row]];
    
        return cell;
    
    }
    

    YMMV, Happy Coding

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

Sidebar

Related Questions

So, my second question based off of this application I'm teaching myself Objective C
This question is based off some really odd code I recently found in a
Based off this SO question about counting forgein keys , I have a problem.
This question is being based off of a similar question titled, ASP.NET MVC Relative
This question is based off of the same app/source from my previous question which
This question is built off of this previous question 'here' I want to make
I've been trying to solve my question based off of this answer: Populate select
It's a basic website. Based off answers on here, I'm doing this: private $db;
I tried to make an accordion effect with JavaScript based off this video altering
I have a git branch that is based off an old master. This branch

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.