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}
回答2件
あなたの回答
tips
プレビュー