UITableViewにアートワークを表示
動作を確認した環境
環境 | 情報 |
---|---|
Xcode | 6.3.2 (6D2105) |
iOS | 8.3 |
Swift | 1.2 |
Date | 2015/6/6 |
UITableViewにアートワークを表示
曲名やアーティスト名だけだと画面が寂しいので、アートワークを表示するようにしてみる。
単純にアートワークを表示するだけなら意外と簡単。
class PlaylistTableAdmin: NSObject, UITableViewDelegate, UITableViewDataSource { func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellId = "Cell" let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! UITableViewCell let item = g_musicPlayer.playlist[indexPath.row] cell.textLabel?.text = item.title cell.detailTextLabel?.text = "\(item.artist) - \(item.albumTitle)" // アートワークを表示 let size = CGSize(width: 40, height: 40) if let artwork = item.artwork { cell.imageView?.image = artwork.imageWithSize(size) } return cell } ※関係ない部分は省略 }
アートワークが存在するなら、サイズを指定してアートワークを取得。CellのimageViewに設定しているだけ。このままだとアートワークが無い場合に問題がある・・・ あとで対応しよう。
実行した結果はこんな感じになる。
手抜きで済ませるんならこれでいいんだけど、アートワークのサイズが不揃いな場合、見栄えが悪いことになったり。
この問題の対応もまた今度。たぶん。
ソースコード
最終的なソースコードはこちら。
See_Ku / MusicPlayerTips — Bitbucket
https://bitbucket.org/See_Ku/musicplayertips
https://itunes.apple.com/jp/app/four-album-shuffle-arubamu/id866046150?mt=8&uo=4&at=10l8JW&ct=hatenablog