【iPhoneプログラム】テーブルの利用 調査

UITableViewControllerを使ってテーブルを作る。

 

テーブルというのは『ミュージック』など

さまざまなiPhoneアプリで利用されている以下の一覧形式のもの。

 

Default

 

InterfaceBuilderを使用せずにテーブル作る方法を調べていて見つかったのが以下

 

#import <UIKit/UIKit.h>

@interface HelloController : UITableViewController
@end

@implementation HelloController

- (HelloController *) init
{
    if (self = [super init]) self.title = @"Fonts";
    return self;
}

#pragma mark UITableViewDataSource Methods

// Only one section in this table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// Return how many rows in the table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[UIFont    familyNames] count];
}

// Return a cell for the ith row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Use re-usable cells to minimize the memory load
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"any-cell"];
    if (!cell) cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"any-cell"] autorelease];
    // Set up the cell's text
    cell.text = [[UIFont familyNames] objectAtIndex:[indexPath row]];
    return cell;
}

#pragma mark UITableViewDelegateMethods

// Respond to user selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
    printf("User selected row %d\n", [newIndexPath row] + 1);
    // Update the Navigation Item with the new font
    NSString *fontName = [[UIFont familyNames] objectAtIndex:[newIndexPath row]];
    CFShow([UIFont fontNamesForFamilyName:fontName]);
    [(UILabel *)self.navigationItem.titleView setFont:[UIFont fontWithName: fontName size:[UIFont systemFontSize]]];
}

- (void)loadView
{
    [super loadView];
    self.navigationItem.titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 4.0f, 320.0f, 36.0f)] autorelease];
    // Prepare the Navigation Item
    [(UILabel *)self.navigationItem.titleView setText:@"Font Families"];
    [(UILabel *)self.navigationItem.titleView setBackgroundColor:[UIColor clearColor]];
    [(UILabel *)self.navigationItem.titleView setTextColor:[UIColor whiteColor]];
    [(UILabel *)self.navigationItem.titleView setTextAlignment:UITextAlignmentCenter];
    [(UILabel *)self.navigationItem.titleView setFont:[UIFont boldSystemFontOfSize:[UIFont systemFontSize]]];
}

@end

@interface SampleAppDelegate : NSObject <UIApplicationDelegate>
{
    UINavigationController *nav;
}
@property (nonatomic, retain)        UINavigationController *nav;
@end

@implementation SampleAppDelegate
@synthesize nav;
- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];
    [window addSubview:self.nav.view];
    [window makeKeyAndVisible];
}

- (void) dealloc
{
    [self.nav release];
    [super dealloc];
}
@end

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
    [pool release];
    return retVal;
}

Fontの一覧を出力する例。

 

loadViewメソッド部で親クラスのloadViewを呼び出している部分に疑問を感じる。

書き方が強引では?

 

3種類のtableViewメソッドに関しても、わかりにくい。

もっと楽に使いたい。

 

引き続き調査。

コメント