Array in array

Discussion in 'iOS Programming' started by nicoko, Jan 11, 2009.

  1. nicoko
    Expand Collapse
    macrumors newbie

    Joined:
    Jan 6, 2009
    #1
    Hi,

    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?

    Thanks,

    Nico
     
  2. xsmasher
    Expand Collapse
    macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    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:

    Code:
    	//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. nicoko
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jan 6, 2009
    #3
    Re: An array of arrays

    Thanks a lot!

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

    Code:
    - (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?

    Thanks,

    Nico
     
  4. xsmasher
    Expand Collapse
    macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #4
    Same way, using "count" - just get the list of names from allCompanyEmployees, and count it.

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

    Joined:
    Jan 6, 2009
    #5
    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. xsmasher
    Expand Collapse
    macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #6
    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