新しいストーリーボードからビューコントローラのインスタンスを生成する。
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIViewController *viewController; @end
#import "AppDelegate.h" #import "ViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([ViewController class]) bundle:nil]; ViewController *viewController = [storyboard instantiateInitialViewController]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.viewController = navigationController; self.window.rootViewController = navigationController; [self.window makeKeyAndVisible]; return YES; } @end
選択行をスムーズに消すために、viewWillAppear:イベントメソッド内でdeselectRowAtIndexPath: animated:メソッドを呼び出す。
セルを再利用のためにdequeueReusableCellWithIdentifier:メソッドを使う。
画面遷移については、didSelectRowAtIndexPath:イベントメソッド内で実行する。
#import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @end
#import "ViewController.h" #import "DetailViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController { @private NSArray *_leagueKinds; NSArray *_leagues; NSArray *_leaguePrefixes; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _leagueKinds = @[@"セントラル・リーグ", @"パシフィック・リーグ"]; _leagues = @[ @[@"ジャイアンツ", @"タイガース", @"ドラゴンズ", @"ベイスターズ", @"カープ", @"スワローズ"], @[@"バッファローズ", @"ホークス", @"ファイターズ", @"マリーンズ", @"ライオンズ", @"ゴールデンイーグルス"] ]; _leaguePrefixes = @[ @[@"読売", @"阪神", @"中日", @"横浜DeNA", @"広島東洋", @"東京ヤクルト"], @[@"オリックス", @"福岡ソフトバンク", @"北海道日本ハム", @"千葉ロッテ", @"埼玉西武", @"東北楽天"] ]; self.title = @"日本プロ野球チーム"; self.tableView.dataSource = self; self.tableView.delegate = self; } - (void)viewWillAppear:(BOOL)animated { [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [_leagueKinds count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [_leagues[section] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } NSArray *teams = _leagues[indexPath.section]; NSArray *teamPrefixes = _leaguePrefixes[indexPath.section]; cell.textLabel.text = teams[indexPath.row]; cell.detailTextLabel.text = teamPrefixes[indexPath.row]; return cell; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { static NSString *HeaderIdentifier = @"Header"; UITableViewHeaderFooterView *view = [tableview dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier]; if (!view) { view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier]; } view.textLabel.text = _leagueKinds[section]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *teams = _leagues[indexPath.section]; NSArray *teamPrefixes = _leaguePrefixes[indexPath.section]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([DetailViewController class]) bundle:nil]; DetailViewController *detailViewController = [storyboard instantiateInitialViewController]; [detailViewController setTitle:teams[indexPath.row]]; [detailViewController setDetailItem:teamPrefixes[indexPath.row]]; [self.navigationController pushViewController:detailViewController animated:YES]; } @end
[参考URL]
iOS View Controllerプログラミングガイド
0 件のコメント:
コメントを投稿