Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nicoko

macrumors newbie
Original poster
Jan 6, 2009
4
0
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
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
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.
 

nicoko

macrumors newbie
Original poster
Jan 6, 2009
4
0
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
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
But how to know how much rows a company has?

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];
 

nicoko

macrumors newbie
Original poster
Jan 6, 2009
4
0
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?
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
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?

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.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.