『Audio帳(仮)』開発 その4 オーディオ一覧の実装
オーディオ一覧を表示するViewControllerを実装する
AlbumListViewControllerでアイテムを選択したときにオーディオ一覧を表示するViewControllerを実装したい。
オーディオ一覧なのでクラス名は「AudioListViewController」にしておく。
initメソッドの実装
このクラスのinitメソッドを実装する。
一覧として表示するMPMediaItemCollectionをメンバ変数として保持するので、initメソッドの引数にする。
|
1 2 3 4 5 6 7 8 9 10 11 |
@class MPMediaItemCollection;
@interface AudioListViewController : UITableViewController {
}
@property (nonatomic, retain) MPMediaItemCollection* collection;
- (id)initWithCollection:(MPMediaItemCollection*)collection;
@end |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
@implementation AudioListViewController
@synthesize collection=_collection;
- (id)initWithCollection:(MPMediaItemCollection*)collection
{
self = [super init];
if (self) {
self.collection = collection;
}
return self;
} |
ナビゲーションのタイトルをアルバム名にする
initWithCollectionメソッド内でNavigationBarのTitleを設定しておく。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- (id)initWithCollection:(MPMediaItemCollection*)collection
{
self = [super init];
if (self) {
self.collection = collection;
MPMediaItem *representativeItem = [self.collection representativeItem];
// タイトルを表示
NSString *title = [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
if ([title isEqualToString:@""]) {
title = @"不明なアルバム";
}
self.navigationItem.title = title;
}
return self;
} |
タイトルが空なら「不明なアルバム」を表示しておく。
Table view data sourceを実装
sectionは1で固定。rowはコレクションのアイテム数を返しておく。
|
1 2 3 4 5 6 7 8 9 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.collection.count;
} |
cellにはアイテムのタイトルとアーティストを表示するようにする。
なので、cellのtypeはUITableViewCellStyleSubtitleにしておく。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// アイテムをゲット
MPMediaItem *item = [self.collection.items objectAtIndex:indexPath.row];
// タイトル表示
cell.textLabel.text = [item valueForProperty: MPMediaItemPropertyTitle];
// アーティスト表示
cell.detailTextLabel.text = [item valueForProperty: MPMediaItemPropertyArtist];
return cell;
} |