[iPhone]プロパティリストを読み込む NSDictionary



UITableViewにbeatportのジャンルのリストを表示するのに一番スマートな方法を模索していていい感じにやる方法がわかったのでメモ。

ベタにコードに書かずにプロパティリストエディタでgenre.plistを作成しリソースに追加する。

plist

テーブルビューに表示する名称の配列とジャンルのデータを定義する

@interface RootViewController : UITableViewController {
NSArray*		names;
NSDictionary *dictGenre;
}
@property (retain) NSArray*		names;
@property (retain) NSDictionary* dictGenre;

プロパティリストファイルをNSDictionaryクラスに読み込み、キー一覧をソートしてNSArrayオブジェクトへ展開する迄

//		Genre.plistの読み込み
NSString *pathGenre = [[NSBundle mainBundle] pathForResource:@"Genre" ofType:@"plist"];

//		RootViewControllerを取得
RootViewController* rootViewController;
rootViewController = (RootViewController*)navigationController.topViewController;

rootViewController.title = @"Grenres";
rootViewController.dictGenre = [NSDictionary dictionaryWithContentsOfFile:pathGenre];

//	NSDicronayのkeyを配列に取り出してソート
NSArray *sortedArray =[[rootViewController.dictGenre allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
rootViewController.names = sortedArray;

UITableViewのセル描写のコールバックでソートしたジャンル名を表示してやる。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [names count];
}

- (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];
    }
  	cell.text = [names objectAtIndex:indexPath.row];
    return cell;
}

ウホッ
grenre
tAkatronixおすすめのiPhone開発本

iPhone デベロッパーズ クックブック
Erica Sadun
ソフトバンククリエイティブ
売り上げランキング: 84906
詳解 Objective-C 2.0
詳解 Objective-C 2.0
posted with amazlet at 10.01.31
荻原 剛志
ソフトバンククリエイティブ
売り上げランキング: 4163
iPhoneプログラミングUIKit詳解リファレンス
所 友太
リックテレコム
売り上げランキング: 3089
iPhone Core Audioプログラミング
永野 哲久
ソフトバンククリエイティブ
売り上げランキング: 22615
Bookmark and Share

関連記事

  1. #1 by Mike on 2009/07/13 - 2:52 PM

    I got jacked on usfreeads by some jerkwad using a mobile line. Jerkwad was shocked when I got her addy info and came calling lol!

    Reverse Number Lookup

(will not be published)