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

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

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

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

Q&A

解決済

1回答

2066閲覧

storyboardなしでテーブルビューを作りたい

taro_nii_chan

総合スコア207

Swift

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

0グッド

1クリップ

投稿2017/10/13 09:30

編集2017/10/13 09:53

やりたい事

クラス定数(配列)をテーブルビューに表示したい

やった事

Single View Appを作った後、Main.storyboardを消し、Info.plistの該当部分を消した上で実行し、エラーが出ないことを確認しました。その後、下記のコードを書きました。

困っていること

AppDelegate.swiftの頭でエラーが発生します

やった事

以下のコードを書きました

swift

1// 2// AppDelegate.swift 3// Foo 4// 5import UIKit 6 7@UIApplicationMain 8class AppDelegate: UIResponder, UIApplicationDelegate { 9 10 var window: UIWindow? 11 12 13 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 14 // Override point for customization after application launch. 15 16 window = UIWindow(frame: UIScreen.main.bounds) 17 window!.backgroundColor = UIColor.white 18 window!.rootViewController = TableViewController() 19 window!.makeKeyAndVisible() 20 21 return true 22 } 23 24 (中略) 25 26}

swift

1// 2// ViewController.swift 3// Foo 4// 5import UIKit 6 7class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 8 9 let animals = ["サル", "キジ", "犬", "桃太郎"] 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 let tableView = UITableView(frame: view.frame, style: .plain) 15 tableView.rowHeight = 70 16 tableView.register(UITableView.self, forCellReuseIdentifier: "cell") 17 tableView.delegate = self 18 tableView.dataSource = self 19 view.addSubview(tableView) 20 } 21 22 func numberOfSections(in tableView: UITableView) -> Int { 23 return 1 24 } 25 26 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 return animals.count 28 } 29 30 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 31 let cell:UITableViewCell = UITableViewCell(style: .default, reuseIdentifier: "cell") 32 cell.textLabel!.text = animals[indexPath.row] 33 34 return cell 35 } 36 37 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 38 //セルの選択解除 39 tableView.deselectRow(at: indexPath, animated: true) 40 41 //ここに遷移処理を書く 42 // self.present(SecondViewController(), animated: true, completion: nil) 43 } 44 45 override func didReceiveMemoryWarning() { 46 super.didReceiveMemoryWarning() 47 // Dispose of any resources that can be recreated. 48 } 49}

エラー

libc++abi.dylib: terminating with uncaught exception of type NSException

エラーを追記します

2017-10-13 18:13:18.167840+0900 Foo[2982:775878] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewCell'
*** First throw call stack:
(0x1869b3d38 0x185ec8528 0x1869b3c80 0x18feb4784 0x100b0dd04 0x100b0e3ac 0x18fdc3bfc 0x18fdc37d4 0x18fdca39c 0x18fdc7608 0x18fe37068 0x100b0fb5c 0x100b0fe40 0x18fe33050 0x190026898 0x19002b6e4 0x1902b9454 0x1905891f0 0x1902b90b8 0x1902b9928 0x190a226e8 0x190a2258c 0x19079e9c0 0x190933fc8 0x19079e870 0x190588850 0x190029e28 0x19042d6ec 0x189055768 0x18905e070 0x10197945c 0x101985b74 0x189089a04 0x1890896a8 0x189089c44 0x18695c358 0x18695c2d8 0x18695bb60 0x186959738 0x18687a2d8 0x18870bf84 0x18fe27880 0x100b10834 0x18639e56c)
libc++abi.dylib: terminating with uncaught exception of type NSException

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

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

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

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

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

MasakiHori

2017/10/13 09:38

エラーの情報が足りません。すべてを記入してください。
guest

回答1

0

ベストアンサー

viewDidLoad()

swift

1 tableView.register(UITableView.self, forCellReuseIdentifier: "cell")

の第一引数はUITableViewCellまたはそのサブクラスのタイプである必要があります。

投稿2017/10/13 10:06

MasakiHori

総合スコア3384

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

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

taro_nii_chan

2017/10/13 10:19

UITableViewをUITableViewCellに変えたら一発でした。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問