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

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello,
I have an array with objects to be displayed in a tableview, lets say the objects are cars. Those cars all have a type and a category. Type could be BMW and category cabriolet for instance.

The table will have sections which are the same as the car type and in every section the categories for this specific type should be listed. So I thought of something like this. The problem is - how do I manage to show the category value in the table cell?

My code so far:
Code:
//init and sorting the objects in the array into dict
for(Car *c in self.allCars){
		if(![sectionNameArray containsObject:c.type]){
			[sectionNameArray addObject:c.type];
			[sectionNameDict setObject:[NSMutableDictionary dictionary] forKey:c.type];
		}
	}
	
	
	for(Car *c in self.allCars){//loop through all cars
		if([[sectionNameDict allKeys] containsObject:c.type]){//look if there is a key in the sectionNameDict == the current car type
			//[[sectionNameDict objectForKey:c.type] addObject:c]; //for every key in dict an array is created. If the current object type is the same as the key -> add the current car to this array
			if(![[[sectionNameDict objectForKey:c.type] allKeys] containsObject:c.category]){//if the dictionary has NOT a key == c.category
				[[sectionNameDict objectForKey:c.type] setObject:[NSMutableArray array] forKey:c.category];//create an array to be saved on this post in the dictionary
				[[[sectionNameDict objectForKey:c.type] objectForKey:c.category] addObject:c];
			}
			else {//there is an array for this key and we can add the current object to the array
				[[[sectionNameDict objectForKey:c.type] objectForKey:c.category] addObject:c];
			}
		}
	}


//table delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [self.sectionNameArray count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	TableHeaderViewController *headerView = [[[TableHeaderViewController alloc]initWithNibName:@"TableHeaderViewController" bundle:nil] autorelease];
	headerView.headlineText = [self.sectionNameArray objectAtIndex:section];
	NSLog(@"[self.sectionNameArray objectAtIndex:section] %@",[self.sectionNameArray objectAtIndex:section]);
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[self.sectionNameDict objectForKey:[self.sectionNameArray objectAtIndex:section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)t cellForRowAtIndexPath:(NSIndexPath *)indexPath {	
//how do I get the value for the lable to show the category in the cell?????
}

Thanks in advance for any help!
MACloop
 
I would suggest storing your datasource in a nested-array: top-level array contains the sections (in your case, types), second-level array contains the rows in each section (in your case, categories). You will find this approach is quite common and there are a number of examples of out there using it.
 
I would suggest storing your datasource in a nested-array: top-level array contains the sections (in your case, types), second-level array contains the rows in each section (in your case, categories). You will find this approach is quite common and there are a number of examples of out there using it.

Thanks for the answer. I came up with a solution like that yesterday and it seems to work.
MACloop
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.