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

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

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

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

Swift

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

Q&A

解決済

1回答

999閲覧

UITableViewを複数並べたい

退会済みユーザー

退会済みユーザー

総合スコア0

Xcode

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

Swift

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

0グッド

0クリップ

投稿2021/12/17 05:09

前提・実現したいこと

画像のように UITableViewを複数(2つ)並べたいです。

イメージ説明

複数並べるとなぜかアプリがクラッシュします。
UITbaleViewを並べる際は1個と2個の時では何か違うのでしょうか??

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

Thread 1: "unable to dequeue a cell with identifier customCell01 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

該当のソースコード

@IBOutlet var tableView: UITableView! @IBOutlet weak var tableView02: UITableView! override func viewDidLoad() { super.viewDidLoad() ... tableView.delegate = self tableView.dataSource = self tableView02.delegate = self tableView02.dataSource = self // カスタムセル登録 tableView.register(UINib(nibName: "EditListTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell01") tableView02.register(UINib(nibName: "ListCell", bundle: nil), forCellReuseIdentifier: "customCell") ... // MARK: - UITableView func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.pageInfos.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "customCell01", for: indexPath) as! EditTwitterListTableViewCell let url = URL(string:pageInfos[indexPath.row].imageUrl) Nuke.loadImage(with: url, into: cell.iconImage) cell.name.text = pageInfos[indexPath.row].name cell.url.text = pageInfos[indexPath.row].url return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 } func tableView02(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView02(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "customCell01", for: indexPath) as! EditTwitterListTableViewCell cell.name.text = "name" cell.url.text = "url" return cell } func tableView02(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100 }

試したこと

・セルの identifier の接続確認
イメージ説明
・UITbaleViewのOutlet確認
など

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

UITableViewDataSource や UITableViewDelegate のメソッドですが、tableView に対しては func tableView(〜)、tableView02 に対しては func tableView02(〜) というように呼び分けられることはありません。すべての UITableView に対して、func tableView(〜) という同じ名前のものが呼ばれます。

ので、それぞれのメソッドの中で、引数で渡された tableView に応じて場合分けする必要があります。
なお、if tableView == self.tableView { の左辺 tableView は引数で渡された処理対象の UITableView で、右辺 self.tableView はプロパティの tableView です。(tableView01 という名前の方が分かりやすいかと…。)
また、tableView と tableView02 では、セルの型や reuse identifier が違うのでは。

swift

1 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 if tableView == self.tableView { 3 // tableView の列数 4 return self.pageInfos.count 5 } else { 6 // tableView02 の列数 7 return 1 8 } 9 } 10 11 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 12 if tableView == self.tableView { 13 // tableView のセル 14 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell01", for: indexPath) as! EditTwitterListTableViewCell 15 16 let url = URL(string:pageInfos[indexPath.row].imageUrl) 17 Nuke.loadImage(with: url, into: cell.iconImage) 18 19 cell.name.text = pageInfos[indexPath.row].name 20 cell.url.text = pageInfos[indexPath.row].url 21 22 return cell 23 } else { 24 // tableView02 のセル 25 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! ListCell 26 27 cell.name.text = "name" 28 cell.url.text = "url" 29 30 return cell 31 } 32 } 33 34 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 35 if tableView == self.tableView { 36 // tableView のセルの高さ 37 return 100 38 } else { 39 // tableView02 のセルの高さ 40 return 100 41 } 42 }

投稿2021/12/17 10:57

hoshi-takanori

総合スコア7895

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

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

退会済みユーザー

退会済みユーザー

2021/12/17 15:26 編集

回答ありがとうございます。 前半理解しました。 後半部分に「 また、tableView と tableView02 では、セルの型や reuse identifier が違うのでは。 」はおっしゃる通りです。 どうすれば良いでしょうか?
hoshi-takanori

2021/12/17 15:29

回答のコードは既に対応したつもりですが、cell の型が ListCell じゃなかったら実際の型に変更してください。 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! ListCell
退会済みユーザー

退会済みユーザー

2021/12/17 15:31

そこは変更してあります。 それでも何故か落ちます、、 エラーは同じ内応なので identifier の登録を指摘されています。 登録は... tableView.register(UINib(nibName: "カスタムセルファイル名", bundle: nil), forCellReuseIdentifier: "identifier名") だけではダメなのでしょうか??
退会済みユーザー

退会済みユーザー

2021/12/17 15:33

stroyboradのUITableViewの中のcellのidentifierも設定しています。 datasourceなども紐づけています。。
退会済みユーザー

退会済みユーザー

2021/12/17 15:39

あっ、、自己解決しました。。。 明日回答載せます。夜遅くまで回答いただきありがとうございますm(_ _)m
退会済みユーザー

退会済みユーザー

2021/12/21 08:48

override func viewDidLoad() { super.viewDidLoad()...}内のカスタムセル登録部分に微妙な判定ミスがあったのが原因でした。 ベストアンサーはシンプルで読みやすいこちらの回答とさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問