きのうは水とまつげしか食べてません

きのうは水とまつげしか食べてません

旅行や街歩きのオトクな情報をご紹介。3分で読み切れる記事を書いていきます。

「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【五日目】

いろいろなことがあって全く更新できずにいました。

詳しいことについてはまた別の記事で。

 

さて、五日目です。

 

今までの記事はこちら

「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【一日目】 - きのうは水とまつげしか食べてません

「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【二日目】 - きのうは水とまつげしか食べてません

「10日でおぼえる iPhoneアプリ開発入門教室」でiPhoneアプリを作る。【三日目】 - きのうは水とまつげしか食べてません

 

よく見るアプリのかたち

http://www.flickr.com/photos/24509575@N00/71684353

photo by emarschn

 

 チャプター5 テーブルビューの使い方をマスターしよう

  1. テーブルビューにリストを表示する
  2. テーブルビューのリストに画像と説明を加える
  3. テーブルビューからウェブページを開く
  4. テーブルビューの項目をグループ分けする

 

f:id:chiri3526:20140123152232p:image

 

 

こんな感じの一覧表を作る工程をまずは紹介。

 

空のプロジェクトを作る。

f:id:chiri3526:20140124104345p:image

f:id:chiri3526:20140124104359p:image

プロジェクトにビューコントローラを「.xib」ファイル付きで追加。

「File」メニューの「New」から「File...」を選択。これは⌘Nでも可。

そこで、「Objective-C class」を選んで「NEXT」をクリック。

 

f:id:chiri3526:20140124110352p:image

「Subclass of」欄には、「UITableViewController」を選ぶこと。

 

f:id:chiri3526:20140124110411p:image

まずは「.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;

} 

 起動

これで、やっとこよく見るリストが並んだアプリができたよー

f:id:chiri3526:20140124110240p:image

 

 

所感

かなりコードの記述や用語が増えて置いて行かれてる印象。

ここで10日経ってしまったので、

「10日でおぼえる」どころか、読み切ることもできなかったというフヌケ。

 

 

やりきって、アプリを完成させたい。