1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Array in array

Discussion in 'iPhone/iPad Programming' started by nicoko, Jan 11, 2009.

  1. macrumors newbie


    Can someone help me with the following.

    I have a table with sections representing companies. Each section has people working for that company. For example:

    - Apple
    - Steve Jobs
    - Jonathan Ive

    - Microsoft
    - Steve Ballmer

    How can I create an array with the data above and access the people of the company's section in cellForRowAtIndexPath?


  2. macrumors regular


    An array of arrays

    Coupla ways.

    You could have two arrays - an array for company names, and a second array for arrays of employees, like so:

    	//company names
    	NSArray *allCompanyNames = [NSArray arrayWithObjects: @"Microsoft",@"Apple",@"IBM",nil];
    	//employee names
    	NSMutableArray *allCompanyEmployees = [[NSMutableArray alloc] init];
    	NSArray *employeeNames;
    	//create an array of employees, and put it in the array of all employees for all companies
    	employeeNames = [NSArray arrayWithObjects: @"Steve Ballmer",nil];
    	[allCompanyEmployees addObject:employeeNames];
    	[employeeNames release];
    	employeeNames = [NSArray arrayWithObjects: @"Steve Jobs", @"Jonathan Ive",nil];
    	[allCompanyEmployees addObject:employeeNames];
    	[employeeNames release];
    	//to get a name out, reverse the process:
    	employeeNames = [allCompanyEmployees objectAtIndex:0];
    	NSString *aName = [employeeNames objectAtIndex:0];
    	NSLog(@"An employee is: %@", aName);
    Note for example's sake I used two different methods to create the arrays - [NSArray arrayWithObjects] and [NSMutableArray addObject] . You could do the whole exercise with either one.
  3. macrumors newbie

    Re: An array of arrays

    Thanks a lot!

    I can display all the sections with the company name, with:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    	return allCompanyNames.count;
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        return [allCompanyNames objectAtIndex:section];
    But how to know how much rows a company has?


  4. macrumors regular


    Same way, using "count" - just get the list of names from allCompanyEmployees, and count it.

    //get the names for employer #0
    NSMutableArray *employeeNames = [allCompanyEmployees objectAtIndex:0];
    //count the names
    int rows = [employeeNames count];
  5. macrumors newbie

    Re: Arrays in Array

    Sorry for being such a noob, but I can't get it to work.

    The code you just sent works perfectly in the viewDidLoad, but not in numberOfRowsInSection.

    Any idea what I'm doing wrong?
  6. macrumors regular


    Not sure. Post your code for those two methods.

    If I had to guess, you might not be retaining the array *that would probably cause a crash,) or you might not be getting the list of names for the right company (in my example, it was always company 0 - but you need the list for the correct section.)

Share This Page