2011年5月12日木曜日

UITableViewController(その1 スタイル)

UITableViewControllerは恐らく一番使うのではないでしょうか。iPhoneアプリではリストで何かを表示するという要求は多いのではないかと思います。

UITableViewControllerにはスタイルがあります。
標準的なリスト表示スタイルと設定画面でよく見かける設定項目毎にグループ化されて見えるスタイルです。

標準的なスタイル


グループスタイル

このような画面を作成してみます。
まずは、クラスの定義ですが、手順はUIViewControllerと同じようにして、クラスを作成します。




UITableViewContorollerは2つの以下のプロトコルを採用しており、その動作をプロトコルの実装を行うことで動作させています。

プロトコル意味
UITableViewDataSourceテーブルのデータモデルを表す
UITableViewDelegateテーブルの動作を表現する

な感じでしょうか。詳細はそれぞれのリファレンスを参照頂くとして、最低限必要なメソッドはスケルトンにて既に実装されていますので、そのコードを修正していきます。
また、データは配列で持つこととします。

ヘッダはこんな感じでデータ格納用のインスタンス変数を定義しておきます。
  1. @interface MyTableViewController : UITableViewController {
  2. //データ格納用配列
  3. NSArray *arrayData;
  4. }
  5. @end

データの初期化は初期化メソッドにでも定義しておきます。

  1. - (id)initWithStyle:(UITableViewStyle)style
  2. {
  3. self = [super initWithStyle:style];
  4. if (self) {
  5. //データの初期化
  6. arrayData = [NSArray arrayWithObjects:@"data1", @"data2", @"data3", @"data4", nil];
  7. }
  8. return self;
  9. }

データの解放コードも忘れずに。

  1. - (void)dealloc
  2. {
  3. //データの解放
  4. [arrayData release];
  5. [super dealloc];
  6. }

UITableViewDataSourceプロトコルの採用メソッド定義

セクションの数を返す。
今回はセクションを一つにしたので、1を返却するのみになります。

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  2. {
  3. // Return the number of sections.
  4. return 1;
  5. }

セクション内の行数を返す。
作成したデータの個数を返すようにします。今回はセクションが一つなので、引数にあるsectionは参照していませんが、
セクションが複数ある場合はセクションに対応したデータの個数を返却するように実装する必要があります。

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  2. {
  3. // Return the number of rows in the section.
  4. return [arrayData count];
  5. }

行(セル)の内容を返す。
セクション、行に対応するデータが必要になった際に呼び出されます。
引数のindexPathにはセクションと行NOの情報が格納されています。

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. //内部的なID。IDとして文字列でセクションNOと行NOを設定しています。
  4. //別に各行でユニークなら何でも良いはずです。
  5. NSString *CellIdentifier = [NSString stringWithFormat:@"section : %d, row no : %d", indexPath.section, indexPath.row];
  6. //一度表示した行(セル)は内部でキャッシュしてくれるので、キャッシュが存在すればそれを使用し、なければ生成するイメージ。
  7. //殆どスケルトンのコードです。
  8. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  9. if (cell == nil) {
  10. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  11. //セルのテキストとして、データの内容とセクションと行数を表示させます。
  12. cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@", [arrayData objectAtIndex:indexPath.row], CellIdentifier];
  13. //ついでにフォントも指定してみる。
  14. cell.textLabel.font = [UIFont fontWithName:@"Arial" size:12.0];
  15. }
  16. // Configure the cell...
  17. return cell;
  18. }

ヘッダーとフッターの内容を返す。
これらのメソッドはスケルトンには含まれていませんでしたので、自分で定義して実装しました。

  1. //ヘッダー
  2. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  3. //セクションが一つなので、そのまま返却していますが、セクションが複数ある場合はセクションに対応する値を
  4. //返却する必要があります。
  5. return @"これはheaderです。";
  6. }
  7.  
  8. //フッター
  9. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  10. //セクションが一つなので、そのまま返却していますが、セクションが複数ある場合はセクションに対応する値を
  11. //返却する必要があります。
  12. return @"これはfooterです。";
  13. }


UITableViewDelegateプロトコルの採用メソッド定義

セルが選択された時の処理を書く。
スケルトンには以下の処理が書かれています。今回は何もしないのでそのまま放置。

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. // Navigation logic may go here. Create and push another view controller.
  4. /*
  5. <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
  6. // ...
  7. // Pass the selected object to the new view controller.
  8. [self.navigationController pushViewController:detailViewController animated:YES];
  9. [detailViewController release];
  10. */
  11. }

作成したTableViewControllerのインスタンス生成。

AppDelegateクラスのdidFinishLaunchingWithOptionsメソッドに記述するイメージです。

標準的なスタイルの場合
myTableViewController = [[MyTableViewController alloc] initWithStyle:UITableViewStylePlain];
//add to view.
[_window addSubview:myTableViewController.view];

グループスタイルの場合
myTableViewController = [[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
//add to view.
[_window addSubview:myTableViewController.view];

0 件のコメント :

コメントを投稿