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

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

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

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

Q&A

解決済

2回答

1775閲覧

TableViewで、データソースから読み込めないと表示される(failed to obtain a cell from its dataSource)

kackey621

総合スコア18

Swift

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

0グッド

0クリップ

投稿2019/04/07 14:05

前提・実現したいこと

「TableViewでTEST1を選択(タップしViewControllerに画面遷移する)したとする。
その時、遷移先のViewControllerで、TEST1と1(配列name2において、TEST1と同じ順番)をUILabelで表示させる。」
上記の内容を実現したいと考えております。

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

2019-04-07 22:53:01.658174+0900 TEST[41654:1641268] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7fbb2b850a00; frame = (0 0; 414 896); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600002f10300>; layer = <CALayer: 0x600002107b40>; contentOffset: {0, -88}; contentSize: {414, 176}; adjustedContentInset: {88, 0, 34, 0}>) failed to obtain a cell from its dataSource (<TEST.TableViewController: 0x7fbb2ac0a290>)' *** First throw call stack: ( 0 CoreFoundation 0x0000000107fd06fb __exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010658bac5 objc_exception_throw + 48 2 CoreFoundation 0x0000000107fd0482 +[NSException raise:format:arguments:] + 98 3 Foundation 0x0000000105fd9927 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194 4 UIKitCore 0x000000010aa8d99f -[UITableView _configureCellForDisplay:forIndexPath:] + 433 5 UIKitCore 0x000000010aaa06bf -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 911 6 UIKitCore 0x000000010aaa0b65 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 73 7 UIKitCore 0x000000010aa68d20 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2870 8 UIKitCore 0x000000010aa88e37 -[UITableView layoutSubviews] + 165 9 UIKitCore 0x000000010ad359c1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1417 10 QuartzCore 0x000000010c2a6eae -[CALayer layoutSublayers] + 173 11 QuartzCore 0x000000010c2abb88 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396 12 QuartzCore 0x000000010c2b7ee4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72 13 QuartzCore 0x000000010c2273aa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328 14 QuartzCore 0x000000010c25e584 _ZN2CA11Transaction6commitEv + 608 15 UIKitCore 0x000000010a880ccb __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 128 16 CoreFoundation 0x0000000107f37aec __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12 17 CoreFoundation 0x0000000107f372b0 __CFRunLoopDoBlocks + 336 18 CoreFoundation 0x0000000107f31b34 __CFRunLoopRun + 1252 19 CoreFoundation 0x0000000107f31302 CFRunLoopRunSpecific + 626 20 GraphicsServices 0x000000010fb692fe GSEventRunModal + 65 21 UIKitCore 0x000000010a867ba2 UIApplicationMain + 140 22 TEST 0x0000000105ca5cfb main + 75 23 libdyld.dylib 0x00000001093d8541 start + 1 24 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

該当のソースコード

Swift

1import UIKit 2 3class TableViewController: UITableViewController { 4 5 //cellのlabelに代入するString 6 let name1:[String] = ["TEST1","TEST2","TEST3","TEST4"] 7 let name2:[String] = ["1","2","3","4"] 8 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 // Uncomment the following line to preserve selection between presentations 14 // self.clearsSelectionOnViewWillAppear = false 15 16 // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 17 // self.navigationItem.rightBarButtonItem = self.editButtonItem 18 } 19 20 //遷移先のViewControllerに渡す変数 21 var giveData: String = "" 22 var giveData2:String = "" 23 24 //sectionの数を返す数 25 override func numberOfSections(in tableView: UITableView) -> Int { 26 return 1 27 } 28 29 // sectionの数を返す関数乗せる 30 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 31 if section == 0{ 32 return name1.count 33 34// }else if section == 1{ 35// return name2.count 36// 37 }else{ 38 return 0 39 40 } 41 42 //sectionの高さを返す 43 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 44 return 40 45 } 46 47 //sectionに載せる文字列を返す関数 48 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 49 return "section(section)" 50 } 51 52 //cellの情報を書き込む関数 53 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 54 let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell 55 56 //ここでcellのlabelに値を入れる 57 if indexPath.section == 0{ 58 cell.name.text = name1[indexPath.item] 59 60// }else{ 61// cell.name.text = name2[indexPath.item] 62// } 63 64 return cell 65 } 66 67 // cellが押されたときに呼ばれる関数 68 // 画面遷移の処理もここで書いている 69 70 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 71 //押された時のcellのlabelの文字列をViewControllerに渡したいので、giveDataに入れる 72 73 if indexPath.section == 0{ 74 giveData = name1[indexPath.item] 75 giveData2 = name2[indexPath.item] 76 77 } 78 print(indexPath) 79 // else{ 80// 81// } 82 83 //Segueを使った画面遷移をおこな関数 84 performSegue(withIdentifier: "Segue", sender: nil) 85 86 87 } 88 89 //遷移先のViewControllerにデータを渡す関数 90 91 func prepare(for segue: UIStoryboardSegue, sender: Any?) { 92 if segue.identifier == "Segue"{ 93 94 let vc = segue.destination as! ViewController 95 vc.receiveData = giveData 96 vc.receiveData2 = giveData2 97 } 98 } 99 100 101 102 // MARK: - Table view data source 103 104// override func numberOfSections(in tableView: UITableView) -> Int { 105// // #warning Incomplete implementation, return the number of sections 106// return 0 107// } 108// 109// override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 110// // #warning Incomplete implementation, return the number of rows 111// return 0 112// } 113 114 /* 115 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 116 let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 117 118 // Configure the cell... 119 120 return cell 121 } 122 */ 123 124 /* 125 // Override to support conditional editing of the table view. 126 override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 127 // Return false if you do not want the specified item to be editable. 128 return true 129 } 130 */ 131 132 /* 133 // Override to support editing the table view. 134 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 135 if editingStyle == .delete { 136 // Delete the row from the data source 137 tableView.deleteRows(at: [indexPath], with: .fade) 138 } else if editingStyle == .insert { 139 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 140 } 141 } 142 */ 143 144 /* 145 // Override to support rearranging the table view. 146 override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 147 148 } 149 */ 150 151 /* 152 // Override to support conditional rearranging of the table view. 153 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 154 // Return false if you do not want the item to be re-orderable. 155 return true 156 } 157 */ 158 159 /* 160 // MARK: - Navigation 161 162 // In a storyboard-based application, you will often want to do a little preparation before navigation 163 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 164 // Get the new view controller using segue.destination. 165 // Pass the selected object to the new view controller. 166 } 167 */ 168 169} 170} 171} 172 173

試したこと

breakpointを打ち、上記のコードから、問題の箇所を探しました。しかし、最後の}でエラーが出たものの、
それより前ではエラーが出ません。
定数や変数の代入には問題が内容に見えます。

補足情報(FW/ツールのバージョンなど)

Xcode Version 10.2 (10E125)

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

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

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

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

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

t_obara

2019/04/07 20:10

}の対応がおかしいです。よく見直してください。XCodeのエディタで確認するとわかりそうですが。
kackey621

2019/04/08 05:10

ありがとうございます。 解決できました。
guest

回答2

0

{}を正しく調整したことで正しくビルドしました。

投稿2019/04/08 05:10

kackey621

総合スコア18

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

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

fuzzball

2019/04/08 05:47 編集

ビルド出来ただけなのでしょうか?実行時エラーは無くなったのですか? また、この回答で解決出来たのですか? それともdsuzukiさんの回答で解決出来たのですか? teratailのルールによると、ベストアンサーは、 >>最も役に立った回答には質問者がベストアンサーをつけられます。 >>質問者が問題解決に至った方法を知ることができます。 とのことですが。
kackey621

2019/04/08 06:11

ご指摘ありがとうございます。 ベストアンサーとさせていただいた回答を含めて 確認し改善した結果、解決できました。 自己解決は、あくまでベストアンサーを補足意味合いです。 最も役だったものは、あくまでベストアンサーです。 ルールに関しては認識しております。 以上の通りご理解いただけると幸いと存じます。
fuzzball

2019/04/08 06:15

質問内の間違いは、質問の編集や追記で行えばいい話で、回答で行うべきではないと思いますが。
kackey621

2019/04/08 06:56

確かにそうかもしれません。 以後気をつけたいと思います。 今回に限っては、このような形で記載することをご了承いただけると 大変幸いと存じます。
guest

0

ベストアンサー

failed to obtain a cell from its DataSource
UITableViewのdequeueReusableCellを使用する時は、その前に以下のfuncで使用するUITableViewCellを登録してください。

Swift

1UITableView 2func register(_ nib: UINib?, forCellReuseIdentifier identifier: String)

投稿2019/04/08 01:01

dsuzuki

総合スコア1682

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問