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

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

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

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

Q&A

解決済

2回答

646閲覧

クロージャー内のTableViewのreloadDataが呼ばれない

oeiqgfodgfhps

総合スコア35

Swift

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

0グッド

0クリップ

投稿2020/07/10 11:53

Githubのリポジトリ検索をした後に、メインスレッドでテーブルビューを読み込み、セルを表示させたいです。
以下のようにプログラムを組んだのですが、ログをだしてりして確認したところtableViewのreloadDataが呼ばれていないことに気づきました。
どのようにコードを改善すればよろしいでしょうか?

⬇️SearchViewController

swift

1import UIKit 2 3class SearchViewController: UIViewController, UITableViewDelegate { 4 5 6 @IBOutlet weak var searchTextField: UITextField! 7 @IBOutlet weak var tableView: UITableView! 8 @IBOutlet weak var searchButton: UIButton! 9 var presenter:SearchViewInput! 10 public override func viewDidLoad() { 11 super.viewDidLoad() 12 initPresenter() 13 tableView.delegate = self 14 } 15 16 func initLayout(){ 17 18 19 } 20 func initPresenter(){ 21 let model = SearchModel() 22 self.presenter = SearchPresenter(view:self,model:model) 23 24 25 26 } 27 28 @IBAction func tappedSearchButton(_ sender: Any) { 29 guard let text = searchTextField.text else{return} 30 presenter.didTappedSearchButton(searchText: text) { 31 DispatchQueue.main.async { 32 self.tableView.reloadData() 33 } 34 } 35 } 36 37 38 39 40} 41 42extension SearchViewController:UITableViewDataSource{ 43 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 44 return presenter.getRepositoryCount() 45 } 46 47 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 48 let data = presenter.getRepositoryForRowAt(indexPath: indexPath.row) 49 let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! RepositoryCell 50 cell.setCell(repository: data) 51 return cell 52 } 53 54 55}

⬇️SearchViewPresenter

swift

1import Foundation 2 3public protocol SearchViewInput:class{ 4 func didTappedSearchButton(searchText:String,complition:@escaping ()->Void) 5 func getRepositoryCount()->Int 6 func getRepositoryForRowAt(indexPath:Int)->RepositoryModel.Repo 7 func test()->String 8} 9 10class SearchPresenter:SearchViewInput{ 11 func test() -> String { 12 return "abc" 13 } 14 15 func getRepositoryCount() -> Int { 16 let count = model.numberRepositories() 17 return count 18 19 } 20 21 22 var view:SearchViewController! 23 var model:SearchModelInput! 24 func didTappedSearchButton(searchText: String, complition: @escaping () -> Void) { 25 26 model.searchRepository(query: searchText, complition: complition) 27 } 28 init (view:SearchViewController,model:SearchModelInput){ 29 self.view = view 30 self.model = model 31 } 32 func getRepositoryForRowAt(indexPath: Int) -> RepositoryModel.Repo { 33 let repository = model.RepositoryForRowAt(IndexPath:indexPath) 34 return repository 35 } 36 37}

⬇️SearchModel

swift

1import Foundation 2 3public struct RepositoryModel:Codable{ 4 var items:[Repo] 5 public struct Repo:Codable{ 6 var name:String! 7 var fullName:String! 8 var description:String! 9 10 private enum CodingKeys:String, CodingKey{ 11 case fullName = "full_name" 12 case name 13 case description 14 } 15 } 16 17} 18protocol SearchModelInput{ 19 20 func searchRepository(query:String,complition:@escaping ()->Void) 21 func numberRepositories()->Int 22 func RepositoryForRowAt(IndexPath:Int)->RepositoryModel.Repo 23 24 25} 26 27 28 29class SearchModel:SearchModelInput{ 30 31 var RepositoriesArray = [RepositoryModel.Repo]() 32 33 34 func searchRepository(query:String,complition: @escaping ()->Void) { 35 let urlRequest = createURLRequest(query: query) 36 print(urlRequest) 37 let decoder = JSONDecoder() 38 let task = URLSession.shared.dataTask(with: urlRequest){ (Data,URLResponse, Error) in 39 do{ 40 let decodedStruct = try decoder.decode(RepositoryModel.self, from: Data!) 41 for i in 0..<decodedStruct.items.count{ 42 self.RepositoriesArray.append(decodedStruct.items[i]) 43 } 44 }catch{ 45 } 46 complition() 47 } 48 49 task.resume() 50 } 51 52 func createURLRequest(query:String)->URLRequest{ 53 let baseURLString = "https://api.github.com/search/repositories" 54 let parameter = query 55 let urlString = baseURLString + "?q=(parameter)" 56 let url = URL(string: urlString)! 57 let urlRequest = URLRequest(url: url) 58 return urlRequest 59 } 60 61 func numberRepositories() ->Int{ 62 return RepositoriesArray.count 63 } 64 func RepositoryForRowAt(IndexPath indexPath:Int) -> RepositoryModel.Repo { 65 return RepositoriesArray[indexPath] 66 } 67}

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

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

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

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

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

fuzzball

2020/07/11 04:08

どのようなログを見て「呼ばれていない」と判断されたのでしょうか? 「呼ばれていない」のか「呼ばれているけど更新されていない」のか、回答者が判断できるような情報を書いてください。
oeiqgfodgfhps

2020/07/11 04:26

cellForRowAtメソッドにprint文を書いてみたりしましたが呼ばれませんでした。 なのでテーブルビューのメソッド自体が呼ばれていないのだと思います
fuzzball

2020/07/11 04:30 編集

「reloadDataが呼ばれていない」ことを確かめるのであれば、reloadDataのところにprint文を入れて確認して下さい。(ようするに、didTappedSearchButtonの中に入っているかどうか)
guest

回答2

0

ベストアンサー

extensionでUITableViewDataSourceの継承はしていますが、
tableView.dataSource = selfが抜けているかと思います。

投稿2020/07/12 10:58

esepatisie

総合スコア62

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

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

0

「reloadData()が呼ばれているけどテーブルが表示されない」という前提での回答になりますが、numberOfSections(in:)を実装していないのでセクション数が0になり、テーブルが表示されないのだと思います。

投稿2020/07/11 04:39

編集2020/07/13 00:16
fuzzball

総合スコア16731

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問