libro
www.tuyano.com
初心者のためのiphone/ipadプログラミング入門

ListViewとナビゲーション (2/5)

作成:2011-04-04 09:02
更新:2011-04-04 09:18

■RootViewControllerクラスをチェック!

では、作成されたコードを見てみましょう。まず、◯◯AppDelegateクラスの方ですが、こちらは通常のアプリのコードとほとんど同じです。まぁ、表示するビューの組み込みが少し大河うやり方だったりしますが、それほど重要なものではないのであまり悩まなくていいでしょう。

今回、最大のポイントは、なんといっても「RootViewConroller」クラスです。.hファイルと.mファイルの中身を下に掲載しておきます(.mファイルの方は、コメントなどを大幅にカットして整理してあります。

まず、見てすぐに気づくのは、このクラスが「UITableViewController」というクラスのサブクラスである、ということでしょう。このUITableViewControllerクラスは、「UITableView」というビューを管理するコントローラークラスです。

画面に表示されているリストは、UIKitの「UITableView」というクラスとして用意されています。そして、これを管理するためのクラスとしてUITableViewControllerが用意されています。リスト表示を行う場合は、これらのクラスを利用するのが基本となります。

.mファイルでは、既に、かなりのメソッドが用意されていることがわかりますね。そのうち、多くはそのままで特に書き方りする必要のないものです。とりあえず、「リストに項目を表示する」という処理を実装するのに必要なメソッドだけ頭に入れておきましょう。
- (NSInteger)tableView:(UITableView *)tableView
        numberOfRowsInSection:(NSInteger)section;
まずは、これです。これは、リストに表示される項目数(行数)を返すメソッドです。returnしたNSIntegerの数だけ項目が用意されます。引数には、項目を表示するUITableViewインスタンスと、セクションの番号を示すNSIntegerが渡されます。
- (UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath;
これは、リストの項目である「セル」を作成し返すものです。リストに表示される各項目は、「UITableViewCell」というクラスのインスタンスとしてUIKitに用意されています。このメソッドでは、リストであるUITableViewインスタンスと、そのセルを表示する場所「インデックスパス」というものを示すNSIndexPathインスタンスというものが引数として渡されます。

とりあえず、この2つのメソッドさえ使えるようになれば、リストに項目を表示させることはできるようになるでしょう。

※プログラムリストが表示されない場合

AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。

●プログラム・リスト●

※RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
}

@end


※RootViewController.m

#import "RootViewController.h"

@implementation RootViewController

- (void)viewDidLoad
	{ [super viewDidLoad];}

- (void)viewWillAppear:(BOOL)animated
	{ [super viewWillAppear:animated]; }

- (void)viewDidAppear:(BOOL)animated
	{ [super viewDidAppear:animated]; }

- (void)viewWillDisappear:(BOOL)animated
	{ [super viewWillDisappear:animated]; }

- (void)viewDidDisappear:(BOOL)animated
	{ [super viewDidDisappear:animated];}

- (NSInteger)numberOfSectionsInTableView:
		(UITableView *)tableView
	{ return 1; }

- (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.
	return cell;
}

- (void)tableView:(UITableView *)tableView 
		didSelectRowAtIndexPath:(NSIndexPath *)
		indexPath
{
    /* コメントアウトされている */
}

/* コメントアウトされているメソッド
 - (BOOL)shouldAutorotateToInterfaceOrientation:
 		(UIInterfaceOrientation)interfaceOrientation {}
 
 - (BOOL)tableView:(UITableView *)tableView 
 		canEditRowAtIndexPath:(NSIndexPath *)
		indexPath {}
 
 - (void)tableView:(UITableView *)tableView commitEditingStyle:
 		(UITableViewCellEditingStyle)editingStyle 
		 forRowAtIndexPath:(NSIndexPath *)indexPath {}
 
 - (void)tableView:(UITableView *)tableView 
 		moveRowAtIndexPath:(NSIndexPath *)
		fromIndexPath toIndexPath:(NSIndexPath *)
		toIndexPath {}
 
 - (BOOL)tableView:(UITableView *)tableView 
 		canMoveRowAtIndexPath:(NSIndexPath *)indexPath {}
 */

- (void)didReceiveMemoryWarning
	{ [super didReceiveMemoryWarning]; }

- (void)viewDidUnload
	{ [super viewDidUnload]; }

- (void)dealloc
	{ [super dealloc]; }

@end
※関連コンテンツ

「初心者のためのiphone/ipadプログラミング入門」に戻る