酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

セパレート付きのUITableViewを使ってみた

セパレート付きのUITableViewを使ってみました。

TableAppDelegate.h

第2セクションに表示する配列を定義しました。

@interface TableAppDelegate : NSObject <UIApplicationDelegate, UITableViewDataSource> {
    NSArray* names;
    NSArray* macNames;
    UIWindow *window;
}

TableAppDelegate.m

@implementation TableAppDelegate

@synthesize window;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch
	
    // namesを初期化
    names = [[NSArray arrayWithObjects:@"iPhone", @"iPod touch", @"iPod nano", nil] retain];
	
	// macNamesを初期化
    macNames = [[NSArray arrayWithObjects:@"iMac", @"Mac Pro", @"Macbook", @"MacBook Pro", nil] retain];
	
    [window makeKeyAndVisible];
	
	return YES;
}

- (void)dealloc {
    [window release];
    [super dealloc];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	// セクションの数を返す
	return 2;	
}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	NSString* sectionTitle = nil;
	
	switch (section)
	{
		case 0:
			sectionTitle = @"iPhone Names";
			break;
		case 1:
			sectionTitle = @"Mac Names";
			break;
		default:
			sectionTitle = nil;
			break;
	}
	
	return sectionTitle;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger cnt = 0;
	
	switch (section)
	{
		case 0:
			cnt = [names count];
			break;
		case 1:
			cnt = [macNames count];
			break;
		default:
			cnt = 0;
			break;
	}
	
	return cnt;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	// セルを作る
	UITableViewCell* cell;
	cell = [tableView dequeueReusableCellWithIdentifier: @"UITableViewCell"];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: @"UITableViewCell"];
		[cell autorelease];
	}

	if (indexPath.section == 0)
	{
		cell.text = [names objectAtIndex:indexPath.row];
	}
	else if (indexPath.section == 1)
	{
		cell.text = [macNames objectAtIndex:indexPath.row];
	}
	
	return cell;
}

@end