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

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回答

2518閲覧

UITableViewDelegate, UITableViewDataSourceが、余分だと言われてしまう。

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/08 11:49

## 余分な、Delegate。

tableViewdequeueReusableCellを使うため、: UITableViewCellを記述した際に、
クラッシュしたため、UITableViewDelegate, UITableViewDataSource, を追加したのですが、
エラーにて「余分なコード」だと言われました。

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

swift

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

## エラー

冗長エラー。

Redundant conformance of 'SearchRootVC' to protocol 'UITableViewDataSource'

クラッシュエラー。

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

## クラッシュ時の、全体コード

swift

1import UIKit 2 3class SearchRootVC: UITableViewController, UISearchBarDelegate { 4 5 @IBOutlet weak var searchBar: UISearchBar! 6 7 var repo: [[String: Any]]=[] 8 9 var task: URLSessionTask? 10 var word: String! 11 var url: String! 12 var idx: Int! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // 元のコード1行削除。Main.storyboardにて、Placeholder使用。 18 19 // UISearchBarのdelegateプロパティに、self(=SearchRootVC)を代入。 20 searchBar.delegate = self 21 } 22 23 // 以下3つ、元から用意されているsearchBar関数名なので、変更NG。 24 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 25 searchBar.text = "" 26 return true 27 } 28 29 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 30 task?.cancel() 31 } 32 33 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 34 35 word = searchBar.text! 36 37 if word.count != 0 { 38 url = "https://api.github.com/search/repositories?q=(word!)" 39 task = URLSession.shared.dataTask(with: URL(string: url)!) { (data, res, err) in 40 if let obj = try! JSONSerialization.jsonObject(with: data!) as? [String: Any] { 41 if let items = obj["items"] as? [[String: Any]] { 42 self.repo = items 43 DispatchQueue.main.async { 44 self.tableView.reloadData() 45 } 46 } 47 } 48 } 49 // これ呼ばなきゃリストが更新されません 50 task?.resume() 51 } 52 53 } 54 55 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 56 if segue.identifier == "Detail" { 57 if let detailVC = segue.destination as? ProfileDetailVC { 58 detailVC.selectedUser = self 59 } 60 } 61 } 62 63 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 64 return repo.count 65 } 66 67 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 68 69 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 70 let rp = repo[indexPath.row] 71 cell.textLabel?.text = rp["full_name"] as? String ?? "" 72 cell.detailTextLabel?.text = rp["language"] as? String ?? "" 73 cell.tag = indexPath.row 74 return cell 75 } 76 77 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 78 // 画面遷移時に呼ばれる 79 idx = indexPath.row 80 performSegue(withIdentifier: "Detail", sender: self) 81 } 82 83}

## 参考サイト

SwiftでTableViewを使ってみよう

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

tableViewでdequeueReusableCellを使うため、: UITableViewCellを記述した際に、

クラッシュしたため、UITableViewDelegate, UITableViewDataSource, を追加したのですが、

この時に出たエラーメッセージは

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

ということですが、カスタムセルは追加されていますでしょうか。
また、そのセルには identifier はつけていますでしょうか。

参考にしているページだと

にそのことがきちんと書かれていますので、今一度ご確認ください。

ちなみに、

Redundant conformance of 'SearchRootVC' to protocol 'UITableViewDataSource'

というエラーも出たということですが、

Swift

1class SearchRootVC: UITableViewController, UISearchBarDelegate { 2

SearchRootVCUITableViewController のサブクラスとして定義していますから、下記引用の太字で示したとおりUITableViewDelegateUITableViewDataSource に準拠させる必要はありません。

Subclass UITableViewController when your interface consists of a table view and little or no other content. Table view controllers already adopt the protocols you need to manage your table view's content and respond to changes. In addition, UITableViewController implements the following behaviors:

投稿2020/09/08 12:29

TsukubaDepot

総合スコア5086

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

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

kazuki_user

2020/09/08 13:13

withIdentifierに正しい識別子を入力していませんでした。。 初歩的な質問にも丁寧にご返答頂き、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問