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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

7354閲覧

TableViewを表示できない

Kenelaoy

総合スコア50

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2017/08/14 05:29

編集2017/08/14 05:37

#問題点
・TableViewを表示できない

Swift

1import Foundation 2import UIKit 3import SwiftyJSON 4import RealmSwift 5 6class TaskViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { 7 @IBOutlet weak var Bar: UINavigationBar! 8 @IBOutlet weak var tableView: UITableView! 9 let ap = UIApplication.shared.delegate as! AppDelegate 10 11 override func viewDidLoad(){ 12 super.viewDidLoad() 13 let Document_path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 14 let fileName = "Test" 15 let path = Document_path + "/" + fileName + ".json" 16 print(path) 17 print(ap.tasks) 18 self.tableView.reloadData() 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 } 24 25 func JsonGet(fileName :String) -> JSON { 26 let Document_path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String 27 let path = Document_path + fileName + ".json" 28 print(path) 29 30 do{ 31 let jsonStr = try String(contentsOfFile: path) 32 print(jsonStr) 33 34 let json = JSON.parse(jsonStr) 35 36 return json 37 } catch { 38 return nil 39 } 40 41 } 42 43 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ //セクションごとの行数を返す 44 return ap.tasks.count 45 } 46 47 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ //各行に表示するセルを返す 48 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) 49 50 // セルに表示する値を設定する 51 cell.textLabel!.text = ap.tasks[indexPath.row] 52 53 return cell 54 } 55 56 func numberOfSections(in tableView: UITableView) -> Int { //セクション数を返す(初期値は1) 57 return 1 //今回は別に何かセクション分けする必要はないので必ず1を返す 58 } 59 60 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { //セクションタイトルを返す(初期値は空) 61 return "" 62 } 63 64 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 65 print("セル番号:(indexPath.row) セルの内容:(fruits[indexPath.row])") 66 }

表示させようとしているデータ自体は、AppDelegateでRealmからデータをforで読み込んで配列化したものです

Swift

1 let data = try! Realm() 2 let BaseData = data.objects(Task.self).sorted(byKeyPath: "priority", ascending: true) 3 4 for i in 0...BaseData.count-1 { 5 self.tasks.append(BaseData[i].TaskName) 6 } 7 8 print(tasks)
tasks = ["テスト2チェック", "テスト4だよ", "こんにちは世界", "", "テストだよ", "テスト2だよ", "テスト3だよ", "テストじゃい!1", "テストじゃち"]

AppDelegateでは

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { } ```の中で処理しているので Realmからデータを読み込んでくる ↓ AppDelegateから配列化されたデータをTableViewが受け取り表示 ということを目論んでいたのですが、実際のTableViewには何も表示されません・・・

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

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

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

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

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

guest

回答1

0

ベストアンサー

UITableViewController を継承していない場合は、 tableView.dataSourceUIViewController で処理することを明示する必要があります。

swift

1tableView.dataSource = self

特別な理由がなければ、 UITableViewController を継承することでこれらの処理をラップしてもらえるので楽できます。

またタップ処理を実装する場合には tableView.delegateUIViewController で処理することを明示する必要もあります。

swift

1tableView.delegate = self

実行時に下記エラーが発生する場合は TableViewCell の設定ができないのが原因です。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

設定する箇所は、Storyboardの TableViewCellIndentifier です。
Cell に対しての情報なので、 TableView ではありません。

イメージ説明

投稿2017/08/14 06:11

編集2017/08/15 01:45
ykws

総合スコア1236

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

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

Kenelaoy

2017/08/14 07:11

ありがとうございます 以下の通り修正したのですが ``` *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' ``` と出てしまっています・・・(ビルド自体は通ります) StoryboardのTableViewのIndentity -> Restoration IDに"MyCell"と設定しているので、問題ないと思ったのですが・・・ ```Swift import Foundation import UIKit import SwiftyJSON import RealmSwift class TaskViewController : UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var Bar: UINavigationBar! @IBOutlet weak var tableView: UITableView! let ap = UIApplication.shared.delegate as! AppDelegate override func viewDidLoad(){ super.viewDidLoad() let Document_path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String let fileName = "Test" let path = Document_path + "/" + fileName + ".json" print(path) print(ap.tasks) tableView.dataSource = self tableView.delegate = self self.tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func JsonGet(fileName :String) -> JSON { let Document_path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String let path = Document_path + fileName + ".json" print(path) do{ let jsonStr = try String(contentsOfFile: path) print(jsonStr) let json = JSON.parse(jsonStr) return json } catch { return nil } } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ //セクションごとの行数を返す return ap.tasks.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ //各行に表示するセルを返す let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) // セルに表示する値を設定する cell.textLabel!.text = ap.tasks[indexPath.row] return cell } func numberOfSections(in tableView: UITableView) -> Int { //セクション数を返す(初期値は1) return 1 //今回は別に何かセクション分けする必要はないので必ず1を返す } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { //セクションタイトルを返す(初期値は空) return "" } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print("セル番号:(indexPath.row) セルの内容:(fruits[indexPath.row])") } func DataDelete(ByDate :Bool) -> String { print("データの削除を行います") var message = "" if ByDate == true{ message = "日時削除完了" }else{ message = "削除完了" } return message } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tableView.reloadData() } } ```
ykws

2017/08/15 01:46

回答を更新しました。
Kenelaoy

2017/08/15 06:14

ありがとうございましたm(_ _)m 無事解決致しました
ykws

2017/08/16 06:31

Good!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問