- You just need to update the tableview datasource before showing these data. For example, you can create a NSDictionary with group names("car", "bike" etc) as the key, a NSArray as the value, so the value for key "car" will be an array of "toyota", "BMW", "Jaguar" objects.
- You can then create a NSArray to hold all the group names in the order you need. groupArray = dataDictionary.allKeys;
- Then you can display all group names and all items under this group:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return groupArray.count;
//section data has only sections e.g. car,bike.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[dataDictionary objectForKey:groupArray[section]] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [groupArray objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSManagedObject *carobj = [[dataDictionary objectForKey:groupArray[indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [carobj valueForKey:@"name"];
return cell;
}