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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

537閲覧

Viewを参照し、VCにデータを引っ張りたい。【MVC】

kazuki_user

総合スコア147

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2020/09/09 00:18

編集2020/09/09 05:25

## タイトル

煩雑なコードを改善する為、

RepositoryCellconfigureCell()を参照し、
SearchRootVCのTableViewにデータを引っ張りたいです。

有効な解決策を教えて頂きたいです。

## コード

SearchRootVC

1 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 3 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: Identifiers.RepositoryCell, for: indexPath) 4 let rp = repo[indexPath.row] 5 6 cell.textLabel?.text = rp["full_name"] as? String ?? "" 7 cell.detailTextLabel?.text = rp["language"] as? String ?? "" 8 cell.tag = indexPath.row 9 return cell 10 }

RepositoryCell

1import UIKit 2 3class RepositoryCell: UITableViewCell { 4 5 override func awakeFromNib() { 6 super.awakeFromNib() 7 // Initialization code 8 } 9 10 func configureCell() { 11 // ここにtextLabel?.textなどの情報を記述 12 } 13}

## 追記

Cell

1import UIKit 2 3class RepositoryCell: UITableViewCell { 4 5 var repo: [[String: Any]]=[] // 必要? 6// 記述しない場合... Use of unresolved identifier 'repo' 7// 記述した場合... No exact matches in call to subscript 8 9 override func awakeFromNib() { 10 super.awakeFromNib() 11 } 12 13 func configureCell(_repo: Dictionary<String, Any>) { 14     // 以下2行にエラー。 15 self.textLabel?.text = repo["full_name"] as? String ?? "" // No exact matches in call to subscript 16 self.detailTextLabel?.text = repo["language"] as? String ?? "" // No exact matches in call to subscript 17 } 18}

質問は以上です。
お時間あるときに、ご返信頂けましたら幸いです????

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

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

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

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

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

guest

回答1

0

ベストアンサー

UITableViewController の tableView:cellForRowAtIndexPath: で、セルにあれこれ細かく指定しているのを、煩わしいので、次のように変えたいということですかね?

列1現状こうしたい
UITableViewControllerデータをもとにセルに表示させる表示させたいデータをセルに渡す
UITableViewCellデータを表示させる

セルが configureCell というメソッドを持っているかどうかは、現状のコードからはわからない状態なので、 cell.configureCell() を呼び出すことは現状できません。
RepositoryCell クラスが configureCell を持っていることはわかっているのですが、
生成しているセルが RepositoryCell クラスのインスタンスであることがわかっていないからです。
まず、次のように、セルは RepositoryCell クラスのインスタンスだよ、ということを明示する必要があります。 (横スクロールを抑えて見やすいように改行しています)

swift

1// この行を置き換える 2// let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: Identifiers.RepositoryCell, for: indexPath) 3let cell: RepositoryCell = tableView.dequeueReusableCell( 4 withIdentifier: Identifiers.RepositoryCell, 5 for: indexPath 6) as! RepositoryCell

どのようにデータを渡すのかは状況次第ですね。テキスト2つくらいであれば String を2つ渡してもいいですし、

swift

1// (横スクロールを抑えて見やすいように適宜改行しています) 2// セル側 3func configureCell(fullName: String, language: String) { 4 self.textLabel?.text = fullName 5 self.detailTextLabel.text = language 6} 7 8// ビューコントローラー側 9override func tableView(_ tableView: UITableView, 10 cellForRowAt indexPath: IndexPath) -> UITableViewCell { 11 12 let cell: RepositoryCell = tableView.dequeueReusableCell( 13 withIdentifier: Identifiers.RepositoryCell, 14 for: indexPath 15 ) as! RepositoryCell 16 17 let rp = repo[indexPath.row] 18 19 cell.configureCell( 20 fullName: rp["full_name"] as? String ?? "", 21 language: rp["language"] as? String ?? "" 22 ) 23 24 cell.tag = indexPath.row 25 return cell 26}

repo[indexPath.row] をそのまま渡してもいいと思いますね。 (型がDictionaryなのかはわかりませんが、参考コードということで……)

swift

1// (横スクロールを抑えて見やすいように適宜改行しています) 2// セル側 3func configureCell(_ repo: Dictionary) { 4 self.textLabel?.text = repo["full_name"] as? String ?? "" 5 self.detailTextLabel.text = repo["language"] as? String ?? "" 6} 7 8// ビューコントローラー側 9override func tableView(_ tableView: UITableView, 10 cellForRowAt indexPath: IndexPath) -> UITableViewCell { 11 12 let cell: RepositoryCell = tableView.dequeueReusableCell( 13 withIdentifier: Identifiers.RepositoryCell, 14 for: indexPath 15 ) as! RepositoryCell 16 17 cell.configureCell(repo[indexPath.row]) 18 19 cell.tag = indexPath.row 20 return cell 21}

投稿2020/09/09 01:21

thyda.eiqau

総合スコア2982

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

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

kazuki_user

2020/09/09 01:52 編集

ご返信ありがとうございます! しかし、No exact matches in call to subscriptが出る箇所があるので、今一度添削して頂けますでしょうか? 型はDictionaryで正しいような気がするのですが... どこか間違っているでしょうか.. コードは「追記」しました
thyda.eiqau

2020/09/09 04:51

とりあえず、var repo: [[String: Any]]=[] // 必要?は不要ですね
kazuki_user

2020/09/09 04:55

ご返信ありがとうございます????‍♂️
kazuki_user

2020/09/09 05:24 編集

質問欄が煩雑になってしまったので、少し編集しました。 現在の状況は、var repo: [[String: Any]]=[] を 記述しない場合... Use of unresolved identifier 'repo' 記述した場合... No exact matches in call to subscript が表示されます。 自分でも調べてみますが、解決策が分かり次第教えて頂けると嬉しいです!
thyda.eiqau

2020/09/09 11:16

func configureCell(_repo: Dictionary<String, Any>) のアンダーバーとrepoの間にスペースがないからでは?
kazuki_user

2020/09/09 11:38

ありがとうございました! 仰る通り、アンダーバーとrepoの間のスペースが原因でした。 引数のアンダーバーについてわかっていないので、調べてみようと思います????
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問