『Audio帳(仮)』開発 その3 アルバム一覧の実装
NavigationBarのタイトル表示
AlbumListViewControllerではオーディオブックの一覧を表示するので「オーディオブック」と表示させる。
|
1 2 3 4 5 6 7 8 |
- (id)init
{
self = [super init];
if (self) {
self.navigationItem.title = @"オーディオブック";
}
return self;
} |
MediaPlayer.FrameWorkを追加
Targetの設定→Build Phases→Link Binary With LibrariesにMediaPlayer.FrameWorkを追加しておく。
iPodライブラリにアクセスするのに必要
TableViewに表示するリストをMPMediaQueryを使って取り出す
MPMediaQueryを使ってオーディオブックのアルバム一覧を取り出す。
取り出した配列はメンバ変数に保持しておこう。
|
1 2 3 4 5 6 7 8 9 |
- (void)updateAlbums
{
// オーディオブックのクエリのみをゲット
MPMediaQuery *query = [MPMediaQuery audiobooksQuery];
// アルバムの一覧が欲しいので、グルーピングはアルバムにする
[query setGroupingType: MPMediaGroupingAlbum];
// コレクションをとっとく
self.albums = [query collections];
} |
Table view data sourceを完成させる
Section数は1のままでいい。
Row数はアルバムの数だけ返すので、メンバ変数に保持している配列の要素数を返しとく。
|
1 2 3 4 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.albums.count;
} |
Cellを返す所では、アルバムのタイトルとアーティストを設定して返すようにする。
タイトルが無い、アーティストが無いアルバムは「不明の〜」にしておく。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
- (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];
}
// アルバム情報をゲット
MPMediaItemCollection *album = [self.albums objectAtIndex:indexPath.row];
MPMediaItem *representativeItem = [album representativeItem];
// タイトルを表示
cell.textLabel.text = [representativeItem valueForProperty: MPMediaItemPropertyAlbumTitle];
if ([cell.textLabel.text isEqualToString:@""]) {
cell.textLabel.text = @"不明なアルバム";
}
// アーティストを表示
cell.detailTextLabel.text = [representativeItem valueForProperty: MPMediaItemPropertyArtist];
if ([cell.detailTextLabel.text isEqualToString:@""]) {
cell.detailTextLabel.text = @"不明な作成者";
}
return cell;
} |
