###前提・実現したいこと
xcode(swift3)で簡単な図鑑のアプリを作っています。今回検索機能をつけようとしたのですが、検索結果とその遷移先の説明文が一致しません。いつも検索から行うと一番上の物の説明文になってしまいます。(下のコードで言うといつもライオンの説明が出てくる)
どう直したらいいのでしょうか?
ちなみにストーリーボードとの接続はできています。
###発生している問題・エラーメッセージ
###該当のソースコード
swift3
1import UIKit 2 3struct AnimalInfo { 4 var name: String 5 var description: String 6} 7 8class ViewController: UIViewController, UITableViewDataSource,UISearchBarDelegate,UIScrollViewDelegate { 9 10 @IBOutlet weak var tableView: UITableView! 11 @IBOutlet weak var searchBar: UISearchBar! 12 13 var searchResults:[AnimalInfo] = [] 14 15 16 let items = [ 17 AnimalInfo(name: "ライオン", description: "百獣の王。一般的に最も強い動物として知られている。"), 18 AnimalInfo(name: "サイ", description: "頭部に硬い角を持っている。巨体に似合わず最高時速50キロで走る。"), 19 AnimalInfo(name: "シマウマ", description: "白黒の縞模様を持つ動物。視覚や嗅覚、聴覚が優れている。"), 20 AnimalInfo(name: "キリン", description: "最も背が高い動物。首が長いところが特徴。"), 21 AnimalInfo(name: "ゾウ", description: "陸生生物では世界最大の動物。鼻は立っていても地面につくほどに長い") 22 ] 23 24 override func viewDidLoad() { 25 super.viewDidLoad() 26 tableView.dataSource = self 27 28 29 30 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "aaaaCell") 31 tableView.delegate = self as? UITableViewDelegate 32 self.view.addSubview(tableView) 33 34 searchBar.delegate = self 35 searchBar.searchBarStyle = UISearchBarStyle.default 36 searchBar.showsSearchResultsButton = false 37 searchBar.placeholder = "検索" 38 searchBar.setValue("キャンセル", forKey: "_cancelButtonText") 39 searchBar.tintColor = UIColor.red 40 41 tableView.tableHeaderView = searchBar 42 } 43 44 override func didReceiveMemoryWarning() { 45 super.didReceiveMemoryWarning() 46 } 47 48 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 49 if searchBar.text != "" { 50 return searchResults.count 51 } else { 52 return items.count 53 } 54 } 55 56 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 57 let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "NameCell") 58 let item = items[indexPath.row] 59 60 61 if searchBar.text != "" { 62 cell.textLabel!.text = "\(searchResults[indexPath.row].name)" 63 } else { 64 cell.textLabel!.text = "\(item.name)" 65 } 66 67 return cell 68 } 69 70 // 検索ボタンが押された時に呼ばれる 71 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 72 self.view.endEditing(true) 73 searchBar.showsCancelButton = true 74 self.searchResults = items.filter{ 75 // 大文字と小文字を区別せずに検索 76 $0.name.lowercased().contains(searchBar.text!.lowercased()) 77 } 78 self.tableView.reloadData() 79 } 80 81 // キャンセルボタンが押された時に呼ばれる 82 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 83 searchBar.showsCancelButton = false 84 self.view.endEditing(true) 85 searchBar.text = "" 86 self.tableView.reloadData() 87 } 88 89 // テキストフィールド入力開始前に呼ばれる 90 func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool { 91 searchBar.showsCancelButton = true 92 return true 93 } 94 95 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 96 if let selectedRow = tableView.indexPathForSelectedRow { 97 let controller = segue.destination as! DetailViewController 98 controller.info = items[selectedRow.row] 99 } 100 } 101} 102 103
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
prepare()メソッドの中に、「controller.info = items[selectedRow.row]」としか書いていませんが、searchResultsについても書く必要があるのでは?