質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

354閲覧

Swift MPMediaQueryのデータをUICollectionViewに渡す方法

mitci

総合スコア37

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/12/18 12:07

編集2017/12/19 12:12

###前提・実現したいこと
MPMediaQuery.artists()から下記のコードで取得しているArray<MPMediaQuerySection>()Array<MPMediaItemCollection>()の中に入れた情報がうまいこと取り出せません。

Swift

1func getSectionInfo() -> Array<MPMediaQuerySection> { 2 var sectionArray = Array<MPMediaQuerySection>() 3 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 4 let artistSection = artistQuery.collectionSections! 5 sectionArray = artistSection 6 return sectionArray 7 } 8 9 func getArtistInfo() -> Array<MPMediaItemCollection> { 10 var artistArray = Array<MPMediaItemCollection>() 11 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 12 let artistCollection = artistQuery.collections! 13 artistArray = artistCollection 14 return artistArray 15 }

この手順で取り出した情報を、コレクションビューに表示したくて、まず以下のようにセクションのデータを代入しました。

swift

1//セクションにsectionArray配列から要素を入れていく 2func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 3 var reusableView = UICollectionReusableView() 4 if kind == UICollectionElementKindSectionHeader { 5 let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", for: indexPath) 6 if let label = headerView.viewWithTag(1) as? UILabel { 7 label.text = sectionData[section].title 8 //↑ここがエラーになります 9 } 10 reusableView = headerView 11 } 12 return reusableView 13 }

そして、コレクションビューにもartistArrayから要素を入れました。

swift

1 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 2 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 3 let collection = self.artistData[section.range.location + indexPath.row] 4 //↑ここでもエラーに・・・ 5 cell.textLabel?.text = collection.representativeItem?.artist 6 return cell 7 } 8

###発生している問題・エラーメッセージ
エラーは、

swift

1label.text = sectionData[section].title 2//Cannot subscript a value of type 'Array<MPMediaQuerySection>' with an index of type '(section).Type' (aka 'section.type')

swift

1let collection = self.artistData[section.range.location + indexPath.row] 2//Type 'section' has no member 'range'

です。

Cannot subscript a value of typeはオプショナルな型になっていますよ、という意味かと思い、色々とアンラップをしてみたものの、状況は変わらず、むしろFix Delete '!'と言われてしまい、
2つ目のエラーについては、コレクションビューではなく、テーブルビューに代入した時のコードではちゃんとsection.rangeを発見してくれたため、何が悪いのかがわかりません・・・

もともと、テーブルビューに代入していたものを、はじめてコレクションビューというものを使ってみようと思って書き換えているようなものなので、その差異によるエラーならまだわかりそうなものですが・・・

###追記
sectionの宣言部分を、とのことでしたが、どちらの部分かわからなかったため、全文を載せさせていただきます。

swift

1class ArtistTabView: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 2 3 @IBOutlet weak var artistCollection: UICollectionView! 4 5 var sectionData = Array<MPMediaQuerySection>() 6 var artistData = Array<MPMediaItemCollection>() 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 artistCollection.delegate = self 12 artistCollection.dataSource = self 13 sectionData = getSectionInfo() 14 artistData = getArtistInfo() 15 } 16 17 override func didReceiveMemoryWarning() { 18 super.didReceiveMemoryWarning() 19 // Dispose of any resources that can be recreated. 20 } 21 22 func getSectionInfo() -> Array<MPMediaQuerySection> { 23 var sectionArray = Array<MPMediaQuerySection>() 24 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 25 let artistSection = artistQuery.collectionSections! 26 sectionArray = artistSection 27 return sectionArray 28 } 29 30 func getArtistInfo() -> Array<MPMediaItemCollection> { 31 var artistArray = Array<MPMediaItemCollection>() 32 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 33 let artistCollection = artistQuery.collections! 34 artistArray = artistCollection 35 return artistArray 36 } 37 38 39 internal func numberOfSections(in collectionView: UICollectionView) -> Int { 40 return self.sectionData.count 41 } 42 43 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 44 return self.sectionData[section].range.length 45 } 46 47 func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 48 var reusableView = UICollectionReusableView() 49 if kind == UICollectionElementKindSectionHeader { 50 let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderView", for: indexPath) 51 if let label = headerView.viewWithTag(1) as? UILabel { 52 label.text = sectionData[section].title 53 } 54 reusableView = headerView 55 } 56 return reusableView 57 } 58 59 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 60 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 61 let collection = self.artistData[section.range.location + indexPath.row] 62 cell.textLabel?.text = collection.representativeItem?.artist 63 return cell 64 } 65 66}

ちなみに、storyboardはこんな感じです。
こんなかんじ
Artist CollectionをIBOutletで接続しており、
その中のCollection Reusable ViewはIdentifierを"HeaderView"にしてあり、
sectionNameラベルはtagを1にしてあります。
Cellの方はIdentifierを"Cell"にしてありますが、
中のArtistNameラベルは特に何も接続していませんし、tagもIdentifierもいじっていません(それもいけないんでしょうか・・・)
ちなみに、Cellはstoryboard上で横幅を341pxに、縦を100pxにしてあります。iPadのランドスケープで3カラムにすることを想定しています。

###補足情報(言語/FW/ツール等のバージョンなど)
xcode8 swift3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MasakiHori

2017/12/18 12:29

sction の宣言部がないとわかりません
mitci

2017/12/19 12:13

情報を追記しました。宣言部がどこかわからなかったので、一応全文なのでお手数なのですが、ご確認お願いします
guest

回答1

0

最初の問いに関して

MPMediaQuerySectionのtitleプロパティの型はstringでしょうか?

================================
二番目の問いに関して

let collection = self.artistData[section.range.location + indexPath.row]
こちらに関しては
sectionとはなんでしょうか?エラーはsectionにrangeプロパティがないことによるエラーかと思われます

簡単に言うと

class User : NSObject {

var name: String = ""

}
のようなものをイメージしまして、

仮にsectionのクラスがHogeとした時

class Hoge: NSObject {

** //これが定義されてない**
var range: ....
}

has no member というのはそれが定義されてないのにアクセスしようとしてますよということです

※プロパティはインスタンス変数とも呼びます

投稿2017/12/27 00:48

komo_ta

総合スコア275

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mitci

2017/12/27 12:01

MPMediaQuerySectionのtitleプロパティはString型です。具体的にいうと、"あ","か","さ"などの各セクションのタイトルです。 2つめにつきましては、sectionが定義されていないことに今、気がつきました・・・ また一つ目のエラー、label.text = sectionData[section].titleに関しても、引数にsectionが含まれていないために出るエラーだとはわかったので,本質的には解決することができました!ただ、label.text = sectionData[section].titleのsectionをIntにし、section += 1で指定したところ、リユースするたびに+1されてしまい、こちらのあたりはまだまだ不完全ですが・・・ すいませんでした。とりあえず、質問に対する回答については、わたしのうっかりミスでした・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問