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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:54:07+00:00 2026-06-01T23:54:07+00:00

I am trying to learn how to code a UITableView and is having some

  • 0

I am trying to learn how to code a UITableView and is having some problems with the programming of the section.

I have declared 3 arrays with strings and 1 array with the 3 arrays.

firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil];
secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil];
thirdSection = [NSArray arrayWithObject:@"Yellow"];

array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];

To shows the headers

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSString * title;
title = [NSString stringWithFormat:@"%@" , [array objectAtIndex:section]];

return title;
}

which shows the array itself as the headers

enter image description here

therefore is it possible to actually show the name of the sections using the names of the arrays such as firstSection and secondSection?

  • 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-01T23:54:09+00:00Added an answer on June 1, 2026 at 11:54 pm

    In your case, it’s better to store your arrays in an NSDictionary. For example, if you declare and synthesize an NSDictionary variable called tableContents and an NSArray called titleOfSections, you can do something like this:

     - (void)viewDidLoad {
        [super viewDidLoad];
    
        //These will automatically be released. You won't be needing them anymore (You'll be accessing your data through the NSDictionary variable)
        NSArray *firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil];
        NSArray *secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil];
        NSArray *thirdSection = [NSArray arrayWithObject:@"Yellow"];
    
        //These are the names that will appear in the section header
        self.titleOfSections = [NSArray arrayWithObjects:@"Name of your first section",@"Name of your second section",@"Name of your third section", nil];
    
        NSDictionary *temporaryDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:firstSection,@"0",secondSection,@"1",thirdSection,@"2",nil];
        self.tableContents = temporaryDictionary;
        [temporaryDictionary release];
    }
    

    Then in the table view controller methods:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [self.titleOfSections count];
    }
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        return [[self.tableContents objectForKey:[NSString stringWithFormat:@"%d",section]] count];
    }
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        //Setting the name of your section
        return [self.titleOfSections objectAtIndex:section]; 
    }
    

    Then to access the contents of each array in your cellForRowAtIndexPath method:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    
        NSArray *arrayForCurrentSection = [self.tableContents objectForKey:[NSString stringWithFormat:@"%d",indexPath.section]];
    
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] 
                     initWithStyle:UITableViewCellStyleSubtitle 
                     reuseIdentifier:SimpleTableIdentifier] autorelease];
        }
        cell.textLabel.text = [arrayForCurrentSection objectAtIndex:indexPath.row];
    
        return cell;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn from google code and I got stuck in some problems.
I am trying to learn javascript i have this code: x=x.replace(/^\s+|\s+$/g,); can i have
I'm trying to learn linked-lists in Java and had some questions about the code
I'm trying to learn C++ currently, but I'm having issues with the code below.
Trying to learn about php's arrays today. I have a set of arrays like
I am newbie trying to learn some code. I am following the tutorial on
I am trying to learn Flex and now i have the next code: http://pastebin.com/rZwxF7w1
Hello I'm 17 and trying to learn how to code. I am having trouble
I'm trying to learn mor eabout C++ and ran into some code in a
I'm trying to learn how QProcess works and have this kind of code: #include

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.