- 2010-03-14 (日) 22:18
- Object-C | iPhone | iPhone SDK 3
「Macを買うなら…」でおなじみの、秋葉館オンラインショップ
もちろん話題のiPodも本体を含め関連商品充実! ![]()

色々と大変そうなので、避けていたのですが、
UITableViewの基礎を試してみました。
こちらをベースに色々カスタマイズしてみようと思います。
- tableView:cellForRowAtIndexPath: メソッド
indexPathが指定する位置のUITableViewCellのインスタンスを返すようにする。 - tableView:numberOfRowsInSection: メソッド
テーブルが保持するセルの数を返すようにする。
iPhone Dev Center: UITableViewDataSource Protocol Referenceより
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title =@"UITableView 聖域十二宮編";
//Tableに表示する要素
items_ = [[NSArray alloc] initWithObjects:
@"1月 山羊座 - Capricornus", @"2月 水瓶座 - Aquarius",@"3月 魚座 - Pisces",
@"4月 牡羊座 - Aries", @"5月 牡牛座 - Taurus", @"6月 双子座 - Gemini",
@"7月 蟹座 - Cancer", @"8月 獅子座 - Leo", @"9月 乙女座 - Virgo",
@"10月 天秤座 - Libra", @"11月 蠍座 - Scorpio", @"12月 射手座 - Sagittarius",
nil];
}
<pre>//Viewのリリースに、要素の配列をリリース</pre>
- (void)viewDidUnload {
[items_ release];
}
// テーブルが持っているセルの数を返す
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [items_ count];
}
// UiTableViewCellのインスタンスを返す
- (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.textLabel.text = [items_ objectAtIndex:indexPath.row];
return cell;
}

セルの選択時にアクションを発生させるには、didSelectedRowAtIndexPathメソッドをオーバライドします。
// セルの選択をハンドリングしてアクションを発生させる
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* message = [items_ objectAtIndex:indexPath.row];
UIAlertView* alert = [[[UIAlertView alloc] init] autorelease];
alert.message = message;
[alert addButtonWithTitle:@"OK"];
[alert show];
}
- Newer: iPhoneアプリ ステータスバーを消す
- Older: iPhoneアプリ 実機テスト手順
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://splitchin.com/tech/2010/03/14/iphone%e3%82%a2%e3%83%97%e3%83%aa%e3%80%80uitableview%e3%82%92%e8%a9%a6%e3%81%99%e3%80%82/trackback/
- Listed below are links to weblogs that reference
- iPhoneアプリ UITableViewを試す。 from あらびき林檎。