「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【五日目】
いろいろなことがあって全く更新できずにいました。
詳しいことについてはまた別の記事で。
さて、五日目です。
今までの記事はこちら
「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【一日目】 - きのうは水とまつげしか食べてません
「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【二日目】 - きのうは水とまつげしか食べてません
「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【三日目】 - きのうは水とまつげしか食べてません
よく見るアプリのかたち
チャプター5 テーブルビューの使い方をマスターしよう
- テーブルビューにリストを表示する
- テーブルビューのリストに画像と説明を加える
- テーブルビューからウェブページを開く
- テーブルビューの項目をグループ分けする
こんな感じの一覧表を作る工程をまずは紹介。
空のプロジェクトを作る。
プロジェクトにビューコントローラを「.xib」ファイル付きで追加。
「File」メニューの「New」から「File...」を選択。これは⌘Nでも可。
そこで、「Objective-C class」を選んで「NEXT」をクリック。
「Subclass of」欄には、「UITableViewController」を選ぶこと。
まずは「.h」ファイルの編集から。
リストの内容を記憶するNSArrayクラスの宣言になる。
#import <UIKit/UIKit.h>
@interface FSRootViewController : UITableViewController
@property (strong, nonatomic) NSArray *models;
@end
次は「.m」の編集。
先ほど宣言した「models」に値をセットするよ。
- (void)viewDidLoad
{
[superviewDidLoad];
self.models = [NSArrayarrayWithObjects:
@"macbook air 11",
@"macbook air 13",
@"macbook pro 13",
@"macbook pro 15",
@"mac mini 2.3GHz",
@"mac mini 2.5GHz",
nil];
}
そして、3つのメソッドを編集する。
これは、テーブルビューからの要求に応じるもの、らしい。
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.models count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.models objectAtIndex:indexPath.row];
return cell;
}
最後に
アプリケーションデリゲードというものの編集。
「FSAppDelegate.m」というファイルに以下のコードを加える。
#import "FSAppDelegate.h"
#import "FSRootViewController.h"
@implementation FSAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
FSRootViewController *myRootViewController = [[FSRootViewControlleralloc]initWithNibName:@"FSRootViewController"bundle:nil];
self.window.rootViewController = myRootViewController;
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}
起動
これで、やっとこよく見るリストが並んだアプリができたよー
所感
かなりコードの記述や用語が増えて置いて行かれてる印象。
ここで10日経ってしまったので、
「10日でおぼえる」どころか、読み切ることもできなかったというフヌケ。
やりきって、アプリを完成させたい。