[ios][memo]UITableViewControllerのめも。

( iPhone )

勉強の一環であります。

実際に実装して間隔つかむ。


#import 

@interface RootViewController : UITableViewController {
    NSMutableArray *arrListSection1;
    NSMutableArray *arrListSection2;
}

@property (nonatomic, retain) NSMutableArray *arrListSection1;
@property (nonatomic, retain) NSMutableArray *arrListSection2;

@end
#import "RootViewController.h"

@implementation RootViewController

@synthesize arrListSection1;
@synthesize arrListSection2;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:@"test table"];
    arrListSection1 = [[NSMutableArray alloc] initWithObjects:@"test1",@"test2",@"test3", nil];
    arrListSection2 = [[NSMutableArray alloc] initWithObjects:@"test11",@"test22",@"test33", nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

//----------------------------------------------------------------
#pragma mark - TableViewController
//----------------------------------------------------------------
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	UIView *view = [[UIView alloc] init];
	// 背景に画像をセットする 
	view.backgroundColor = [UIColor blackColor];
	UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 30.0f)];
	//UIImage *bgImage = [UIImage imageNamed:@"icon.png"];
	//lbl.backgroundColor = [UIColor colorWithPatternImage: bgImage];
    lbl.backgroundColor = [UIColor blackColor];
    lbl.textColor = [UIColor whiteColor];

    if (section == 0) {
        lbl.text = @"first section";
    }
    else if (section == 1) {
        lbl.text = @"second section";
    }
	[view addSubview:lbl];
	[lbl release];
	return view;
}

/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *title = nil;
    if (section == 0) {
        title = @"first section";
    }
    else if (section == 1) {
        title = @"second section";
    }
    return title;
}
 */
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger cnt;
    if (section == 0) {
        cnt = [arrListSection1 count];
    }
    else if (section == 1) {
        cnt = [arrListSection2 count];
    }
    return cnt;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if (indexPath.section == 0) {
        NSString *cellValue = [arrListSection1 objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
        
        NSInteger rowNum = [indexPath row];
        if (rowNum == 0) {
            
        }
        else if (rowNum == 1) {
            
        }
        else if (rowNum == 2) {
            
        }
    }
    if (indexPath.section == 1) {
        NSString *cellValue = [arrListSection2 objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
    }

    // Configure the cell.
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rowNum = [indexPath row];
    NSLog(@"selected [sec %d][%d]", indexPath.section, rowNum);
                        
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

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

@end

できたものはこんなもの

Dave Mark,Jeff LaMarche
ソフトバンククリエイティブ
発売日:2009-12-17