###前提・実現したいこと
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