2009/02/11

[Objective-C][iPhone sdk]表を作成

今度は、iPhoneに表を出力してみたいと思います。
(ファイル名は、output_tableで、Interface Builderを使ってないです。)

ちなみに、純粋に表を出力するエントリーは、asialさんのInterface Builderを使わずに作るiPhoneアプリケーション作成入門というのがあるのですが、文字列が出力されていない点と、入門なのでおそらくそうだと思うのですが、オブジェクト指向ではなく、どちらかというと、プロシージャー指向で書かれていたので、今回は、文字列を出力する、および、オブジェクト指向っぽく書いてみたいと思います。

例によって、処理順にかいていこうかなと。

main.mはかわらずデフォルトのまま

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}

次は、tableの設定を行うset_table.hをみてみます。
#import <UIKit/UIKit.h>


@interface set_table : UITableViewController {
NSArray *names;

}
@property(readwrite,retain) NSArray *names;
@end

表の各セルに入れるデータを格納する変数namesを宣言しています。

また、こやつは、プロパティです。

次、set_tabl.mの実装です。
#import "set_table.h"

@implementation set_table

@synthesize names;

- (id)initWithStyle:(UITableViewStyle)style {
// Override initWithStyle: if you create the controller programmatically and
// want to perform customization that is not appropriate for viewDidLoad.
if (self = [super initWithStyle:style]) {
}
return self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

/* テーブルに表示するセクションの数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
/* 1セクションに何行表示するか */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return 10;
}


// 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] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}

// Set up the cell...
/* セルに値を代入する */
cell.text = [names objectAtIndex:indexPath.row];

return cell;
}

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


@end

initWithStyleメソッドは、このオブジェクトを初期化するときに使われるメソッドで、UITableViewControllerのサブクラスを作成したときに、デフォルトでついてきて、ここでは変更を加えてないです。

同様に、didReceiveMemoryWarningメソッド、deallocメソッドもデフォルトの状態です。

numberOfSectionsInTableViewメソッドでは、セクションの数を決めるのですが、今回は、1に設定します。

tableView:numberOfRowsInSectionメソッドは、1セクション内に、いくつセルを格納するかを、今回は、10でいきます。

後、プロパティを同期させるのに、synthesizeを忘れずに。

で、これが一番、重要なところと思われる、tableView:cellForRowAtIndexPathメソッド。

これは、各セルをどのように設定するかを決めるメソッドだと思われ、おそらく、iPhone sdkが、numberOfSectionsInTableViewで決めたセクション数×tableView:numberOfRowsInSectionで決めた表の数だけコールされるものと思われる。

今回は、1×10で、10回呼び出されます。

基本的には、デフォルトのままなんだけど、追加した部分は、「cell.text = [names objectAtIndex:indexPath.row];」というところ。

これは何かというと、引数の数字(indexPath.row)と同じ番号に入っている配列の値を表のセルに格納している処理を行っています。

ちなみに、表のセル及び配列の最初の番号は、「0」です。
これは、JavaScriptと同じですね!!

また、その上に書かれているif文などの処理は、表のセルの初期化だと思われる。
というか、この部分は、ファイル作成時に自動的に書かれていて変更していないんですよ。

そして、「output_tableAppDelegate.h」を見ていきます。
#import <UIKit/UIKit.h>

@interface output_tableAppDelegate : NSObject {
UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

ここも変更はないです。

最後に、「output_tableAppDelegate.m」です。
#import "output_tableAppDelegate.h"
#import "set_table.h"

@implementation output_tableAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {
set_table *tableViewController =
[[set_table alloc] initWithStyle:UITableViewStylePlain];
tableViewController.names = [[NSArray arrayWithObjects:
@"hoge1"
, @"hoge2"
, @"hoge3"
, @"hoge4"
, @"hoge5"
, @"hoge6"
, @"hoge7"
, @"hoge8"
, @"hoge9"
, @"hoge10"
, nil
]retain];

[window addSubview:tableViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}


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


@end

set_table *tableViewController = [[set_table alloc] initWithStyle:UITableViewStylePlain];
で、変数宣言及び、初期化を行っています。

UITableViewStylePlainは定数で、「ふっつーの表を出力してください」ということを言っています。

次に、tableViewController.names = [[NSArray arrayWithObjects:・・・,nil] retain];は、配列に値を入れる処理かつ、retainメソッドで保持することを指示しています。

ちょっと変わっているのが、arrayWithObjectsメソッドをコールして配列に値をセットするときに、最後に、nilをセットするところ。

これは、リファレンスに書かれていたので、そのまま使いました。

そして、[window addSubview:tableViewController.view];で、表を貼付けています。

今、思ったんだけど、makeKeyAndVisibleメソッドをコールする前に、tableViewControllerのメモリを解放した方がよかったのかもしれない。

実行結果はこちらになります。
Photobucket
今回も自由に転載オッケーです。

0 コメント:

コメントを投稿