PDA

View Full Version : Array in array




nicoko
Jan 11, 2009, 02:51 AM
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
Jan 11, 2009, 08:42 PM
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.

nicoko
Jan 11, 2009, 11:09 PM
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?

Thanks,

Nico

xsmasher
Jan 12, 2009, 12:21 AM
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.


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

nicoko
Jan 12, 2009, 01:13 AM
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
Jan 12, 2009, 11:40 AM
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.)