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

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

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

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

Q&A

解決済

1回答

1812閲覧

Swift テーブルビューが起動してくれない・・・

mitci

総合スコア37

Swift

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

0グッド

0クリップ

投稿2017/12/15 13:14

###前提・実現したいこと
テーブルビューにデバイス上にある曲のアーティストを並べて表示したいのです。
そこで、以下のようにコーディングしました。

swift

1import UIKit 2import MediaPlayer 3 4class ArtistTabView: UIViewController, UITableViewDelegate, UITableViewDataSource { 5 6 @IBOutlet var artistTableView: UITableView! 7 8 var artistSectionData = Array<MPMediaQuerySection>() 9 var artistCollectionData = Array<MPMediaItemCollection>() 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 artistTableView.delegate = self 15 artistTableView.dataSource = self 16 artistSectionData = getSectionInfo() 17 artistData = getArtistInfo() 18 artistTableView.reloadData() 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 // Dispose of any resources that can be recreated. 24 } 25 26 27 func getSectionInfo() -> Array<MPMediaQuerySection> { 28 var array = Array<MPMediaQuerySection>() 29 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 30 let artistSection = artistQuery.collectionSections! 31 array = artistSection 32 return array 33 } 34 35 func getArtistInfo() -> Array<MPMediaItemCollection> { 36 var artistArray = Array<MPMediaItemCollection>() 37 let artistQuery: MPMediaQuery = MPMediaQuery.artists() 38 let artistCollection = artistQuery.collections! 39 artistArray = artistCollection 40 return artistArray 41 } 42 43 44 func numberOfSections(in tableView: UITableView) -> Int { 45 return self.artistSectionData.count 46 } 47 48 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 49 return self.artistSectionData[section].range.length 50 } 51 52 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 53 return self.artistSectionData[section].title 54 } 55 56 func sectionIndexTitles(for tableView: UITableView) -> [String]? { 57 return artistSectionData.map { $0.title } 58 } 59 60 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 62 let section = self.artistSectionData[indexPath.section] 63 let collection = self.artistCollectionData[section.range.location + indexPath.row] 64 cell.textLabel?.text = collection.representativeItem?.artist 65 return cell 66 } 67}

ちなみに、ローンチ画面のボタンを押してタブビューを呼び出し、そこにテーブルビューを置いています。

###発生している問題・エラーメッセージ
上記コードでビルドタイムエラーはでないのですが、
ローンチ画面のボタンを押して上記の「テーブルビューを置いたタブビュー」を表示するタイミングで、コンソールエリアに以下のエラーが出てアプリが落ちます。

unexpectedly found nil while unwrapping an Optional value

デバッグナビゲーターは、

0x100358448 <+120>: brk #0x1 <Thread 1: EXC_BREAKPOINT (code=1, subside-0x100358448)

となっています。
一番最後の行です。

###該当のソースコード

swift

1func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 3 let section = self.artistSectionData[indexPath.section] 4 let collection = self.artistCollectionData[section.range.location + indexPath.row] 5 cell.textLabel?.text = collection.representativeItem?.artist 6 return cell 7 }

let collectionの値がnilになっているのかなと思っていますが、
実際にはよく、わかりません・・・

###試したこと
print()などで監視してみようとしても、表示した瞬間にはアプリが落ちてしまい、コンソールエリアにはunexpectedly found nil while unwrapping an Optional valueがでてしまって・・・

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

そのエラーはOptional型の変数に対して!をつけて必ずnilでないと宣言しているのにnilだった際に発生します。

Swift

1let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!

で取得しているcellがnilなのではないでしょうか。
StoryBoardで"Cell"を登録していない場合、以下のURLのようにtableView.registerで登録する必要があります。
http://xyk.hatenablog.com/entry/2017/01/15/202620

投稿2017/12/15 13:35

編集2017/12/15 13:42
nakasho_dev

総合スコア2655

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

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

mitci

2017/12/17 11:32

確かに、storyboard上で正しくIdentifierが指定されていませんでした・・・ うっかりミスでしたお恥ずかしい・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問