忍者ブログ
2024 / 09
≪ 2024 / 081 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 2024 / 10 ≫

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



section を増やしてみました。

#pragma mark Table view methods
// seciotn の数を 3 にします。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

// Customize the number of rows in the table view.
// Table View の行の数をカスタマイズ と 各 section ごとのCell の数を設定。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch( section ) {
       case 0: return 4;
       case 1: return 3;
       case 2: return 1;
   default: return 0;
}
}
//デフォルトでは存在しないので、以下を丸ごと追加
//section のデータをセットしてます。
- (NSString *) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger) section {

    switch( section ) {
       case 0: return @"果物";
       case 1: return @"野菜";
       case 2: return @"飲み物";
   }
   return nil;
}

// 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];
    }

// Set up the cell...
// section ごとの Cell の中身をセット。
    if( indexPath.section == 0 ) {
          if( indexPath.row == 0 ) {
                cell.text = @"すいか";
          }else if( indexPath.row == 1 ) {
                cell.text = @"メロン";
          }else if( indexPath.row == 2 ) {
                cell.text = @"柿";
          }else if( indexPath.row == 3 ) {
                    cell.text = @"いちご";
          } 
          }else
          if( indexPath.section == 1 ) {
                    if( indexPath.row == 0 ) {
                              cell.text = @"白菜";
                    }else if( indexPath.row == 1 ) {
                              cell.text = @"キャベツ";
                    }else if( indexPath.row == 2 ) {
                              cell.text = @"ねぎ";
                    }
                    } else
                              cell.text = @"ビール";
          return cell;
}

実行〜
ebda72e6.jpg
section の分類項目が入った〜(^^)v
section ごとのデータも入ってるね。

くだらないことだけど、思った通り動くと嬉しいね!













ここまで覚えると、とりあえずテーブルができるね。


PR


とりあえずデータを1個セットしてみる。
データを1個セットするにしても変更箇所が3カ所ある。

section の数、テーブルの Cell の数、実際にセットするデータ。

section の数: numberOfSectionsInTableView
Cell の数 : numberOfRowsInSection
データ  : cell.text = @"にんじん";

cell.text = @"にんじん"; ←だけ追加してみた。
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
// 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];
}
// Configure the cell.
cell.text = @"にんじん";
return cell;
}

実行〜
faeb33d0.jpg
何も表示されず・・・f^^;
numberOfRowsInSection の数が 0 なので表示されず・・・

numberOfRowsInSection を 1 にしてみる。


04e647a2.jpg
にんじんが表示された。


次は、section を表示してみる。








テーブルにデータを入れてみます。
その前にテーブルについて・・・・

これはシュミレータで表示したアドレス帳です。screenshot_02.jpg
「た」とか「A」とか「H」ってやつなんですが、セクションて言うらしいです。

テーブルを作るのには、
・テーブルに表示するデータが必要
・テーブルに表示するデータの分類の数
この分類が、セクション( section )てことになるんでしょうね。

←ちなみに右側、縦にあるやつ。
あ、か、さ、た、な・・・ってやつ!

は sectionIndexTitleForTableView ってやつです。









データを入れてってみます。



タイトル名:TableMaking

タイトルの設定: self.title = @"TableMaking";

RootViewController.m にタイトル名を追加する。

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.title = @"TableMaking";
}


保存して実行!
ed723ef7.jpg

























タイトルが表示された!(^^)v

次は実際にデータを入れてみよ〜っと!



もとまかのiPhone・iPod touch戯れ日記

を参考にしながらテーブルを作ってみようと思います。
基本的なテーブル作成から少しずつカスタマイズしていきたいと思います。
プロジェクト名は tableMaking
a13b04b9.png























screenshot_01.jpg
















MainWindows.xib をクリックします。
screenshot_04.jpg









screenshot_03.jpg







Loaded From "RootViewContoroller"ってことなので RootViewContoroller をみてみます。










screenshot_05.jpg
Table View がありました。









screenshot_06.jpg











とりあえず実行してみます。
screenshot_10.jpg


テーブルが表示されました。
























とりあえずここまでってことで・・・
この後、テーブルに関するいろんな設定を組み込んでいきたいと思います。



08 2024/09 10
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
HN:
cow

自己紹介:
iPhone購入がきっかけでiPhoneアプリの制作にチャレンジ!