Reputation: 2452
I followed the tutorial here and was wondering how to make the table appear grouped.
ex:
group1 contains Subview One and Subview Two
group2 contains Subview Three
I switched the type in interface builder but that only shows one group.
Thanks, Adam
Sidenote* I am a completely new at objective c, hence the tutorial.
EDIT
I thought it might be helpful to put up the piece of code
#import "RootViewController.h"
#import "SubViewOneController.h"
#import "SubViewTwoController.h"
@implementation RootViewController
#pragma mark -
#pragma mark View lifecycle
-(void)awakeFromNib {
views = [[NSMutableArray alloc] init];
SubViewOneController *subViewOneController = [[SubViewOneController alloc] init];
SubViewTwoController *subViewTwoController = [[SubViewTwoController alloc] init];
//Subview 1
subViewOneController.title = @"Subview One";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"Subview One", @"title",
subViewOneController, @"controller",
nil]];
[subViewOneController release];
//Subview 2
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = @"Subview Two";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"Subview Two", @"title",
subViewTwoController, @"controller",
nil]];
[subViewOneController release];
//Subview 3
subViewOneController = [[SubViewOneController alloc] init];
subViewOneController.title = @"Subview Three";
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys:
@"Subview Three", @"title",
subViewOneController, @"controller",
nil]];
[subViewOneController release];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
self.title = @"Basic Navigation";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [views count];
}
//
//I think it goes somewhere in here
//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier];
}
cell.text = [[views objectAtIndex:indexPath.row] objectForKey:@"title"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:@"controller"];
[[self navigationController] pushViewController:targetViewController animated:YES];
}
- (void)dealloc {
[views dealloc];
[super dealloc];
}
@end
Upvotes: 0
Views: 3048
Reputation: 460
Ok so you are correct it does go into your cellForRowAtIndexPath: you will be looking to do something like:
if ([indexPath section] == 0) { //stuff for first section. }
if ([indexPath section] == 1) { //stuff for second section. }
You will need to also deal with how your are configuring the cell from your array. The sections row numbers start with 0. The same thing with didSelectRowAtIndexPath: If you don't deal with the section where you set the cell.text you will end up with
Subview One
Subview Two
Subview One
I recommend getting the iPhone Programming (The Big Nerd Ranch Guide)
Upvotes: 1
Reputation: 94794
This is all controlled by your UITableViewDataSource. The numberOfSectionsInTableView:
method controls how many groups there are, and tableView:numberOfRowsInSection:
controls how many rows are in each group.
Your tableView:cellForRowAtIndexPath:
on the UITableViewDelegate gets an NSIndexPath object; indexPath.section
tells you which group and indexPath.row
tells you which row in the group. The cell you return really has no idea which group it is in or which row in the group it is, it's all controlled by the fact that you return it for a particular indexPath when tableView:cellForRowAtIndexPath:
is called.
Upvotes: 1
Reputation: 50707
In Interface Builder, choose your UITableView object and choose Grouped as the style.
If you are programmatically creating it, use the initWithStyle:
UITableViewStyleGrouped
.
EDIT:
if (section == 0) { /* your first section */ }
if (section == 1) { /* your second section */ }
Upvotes: 1