iPhoneプログラミング: Table Viewを使ってデータを表示する
Table Viewの行にテキスト、イメージ、ディスクロージャボタンを表示
ダウンロード サンプルソース (677.4 KB)
行が選択された場合に別のビューに移動する
Table Viewでユーザーが行を選択した場合に非常によくある動作の1つは、選択された行の詳細を別のビューウィンドウに表示することです。そのためには、新しいXIBファイルとそれに対応するビューコントローラをプロジェクトに追加します。XcodeでClassesフォルダを右クリックし、[Add]→[New File]を選択します。
[Cocoa Touch Class]カテゴリの下で、UIViewControllerサブクラステンプレートを選択し、[With XIB for user interface]オプションが選択されていることを確認して(図10参照)、[Next]をクリックします。
新規クラスにDetailsViewController.mという名前を付け、[Finish]をクリックします。これで、図11のようにXcodeに3つのファイルが追加されます。
DetailsViewController.xibファイルをダブルクリックしてInterface Builderで編集します。[View]ウィンドウに[Label]ビューをドラッグ&ドロップします(図12参照)。
DetailsViewController.hファイルに次のステートメントを入力します。
#import <UIKit/UIKit.h>
@interface DetailsViewController : UIViewController {
IBOutlet UILabel *label;
NSString *textSelected;
}
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) NSString *textSelected;
-(id) initWithTextSelected:(NSString *) text;
@end
次に、DetailsViewController.mファイルに次のステートメントを入力します。
#import "DetailsViewController.h"
@implementation DetailsViewController
@synthesize label;
@synthesize textSelected;
-(id) initWithTextSelected:(NSString *) text {
self.textSelected = text;
[label setText:[self textSelected]];
return self;
}
- (void)viewDidLoad {
[label setText:[self textSelected]];
self.title = @"Movie Details";
[super viewDidLoad];
}
- (void)dealloc {
[textSelected release];
[label release];
[super dealloc];
}
@end
Xcodeでプロジェクトを保存します。Interface Builderに戻り、controlキーを押しながら[File's Owner]アイテムをクリックして、[Label]ビューにドラッグします(図13参照)。ラベルを選択します。
Xcodeに戻り、RootViewController.hファイルに次のステートメントを追加します。
#import "DetailsViewController.h"
@interface RootViewController : UITableViewController {
DetailsViewController *detailsViewController;
}
@property (nonatomic, retain)
DetailsViewController *detailsViewController;
@end
最後に、リスト2のコードをRootViewController.mファイルに追加します。
#import "RootViewController.h"
@implementation RootViewController
@synthesize detailsViewController;
NSMutableArray *listOfStates;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSString *stateSelected = [listOfStates objectAtIndex:
[indexPath row]];
NSString *msg = [[NSString alloc] initWithFormat:
@"You have selected %@", stateSelected];
//---Navigate to the details view---
if (self.detailsViewController == nil)
{
DetailsViewController *d = [[DetailsViewController alloc]
initWithNibName:@"DetailsViewController"
bundle:[NSBundle mainBundle]];
self.detailsViewController = d;
[d release];
}
//---set the state selected in the method of the
// DetailsViewController---//
[self.detailsViewController initWithTextSelected:msg];
[self.navigationController pushViewController:
self.detailsViewController
animated:YES];
[msg release];
}
- (void)viewDidLoad {
//---initialize the array---
listOfStates = [[NSMutableArray alloc] init];
//---add items---
[listOfStates addObject:@"ALABAMA"];
[listOfStates addObject:@"ALASKA"];
//...
[listOfStates addObject:@"WISCONSIN"];
[listOfStates addObject:@"WYOMING"];
self.navigationItem.title = @"States of America";
[super viewDidLoad];
}
- (void)dealloc {
[self.detailsViewController release];
[listOfStates release];
[super dealloc];
}
command+Rキーを押してアプリケーションをテストします。行を選択すると、選択された州を表示する別のビューウィンドウにリダイレクトされるようになっています(図14参照)。
この記事は参考になりましたか?
- japan.internet.com翻訳記事連載記事一覧
- この記事の著者
-
japan.internet.com(ジャパンインターネットコム)
japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.com や EarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
-
Wei-Meng Lee(Wei-Meng Lee)
Microsoft MVP受賞者。Microsoft社の最新テクノロジー実地研修を専門とするDeveloper Learning Solutions社を創設。.NETとワイヤレステクノロジーの開発者、指導者として知られる。国際的なカンファレンスでたびたび講演し、.NET、XML、ワイヤレステクノロジーに関す...
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
この記事は参考になりましたか?
この記事をシェア



![図12 ラベルの付いた新規ビュー:図のように、[View]ウィンドウに[Label]ビューを追加してサイズを変更](http://cz-cdn.shoeisha.jp/static/images/article/4690/12s.gif)
![図13 接続の作成:ラベルアウトレットを[Label]ビューに接続](http://cz-cdn.shoeisha.jp/static/images/article/4690/13s.gif)
