前提・実現したいこと
swift4.2.1
Xcode10.1
section分けされたtableviewにsearchBarを実装したいです。
以下のサイトを参考にしました
https://daisa-n.com/blog/ios-swift-foldingtableview/
発生している問題・エラーメッセージ
以下のコードですと下の写真のように検索した際にitems1のデータしか表示されないようになっています。
func searchBarとfunc tableViewのところをどうにかすればいけるのかなと思っていますが, なに分初心者なものでいまいちよくわからないです。ご教授いただければ幸いです。よろしくお願いします。
該当のソースコード
import UIKit class ViewController:UIViewController,UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate { @IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var tableView: UITableView! var searchActive : Bool = false var items1: NSMutableArray = ["ねずみ", "うし", "とら", "うさぎ", "りゅう"] var items2: NSMutableArray = ["へび", "うま","ひつじ","さる","とり","いぬ","いのしし","ねこ","しまうま"] var items3: NSMutableArray = ["やぎ","くま","しろくま","こぶら","ごりら","ぶた","ぞう","おおかみ"] var section1: Dictionary = [String:NSMutableArray]() var section2: Dictionary = [String:NSMutableArray]() var section3: Dictionary = [String:NSMutableArray]() //全てのセクションタイトルとセルの中身を辞書形式で格納 var sections: Array = [Dictionary<String,NSMutableArray>]() var filtered:[String] = [] override func viewDidLoad() { super.viewDidLoad() // セクションのタイトルとデータの配列を設定する。//辞書 section1 = ["あ":items1] section2 = ["か":items2] section3 = ["さ":items3] // セクションを配列に設定する。 sections.append(section1) sections.append(section2) sections.append(section3) //デリゲート設定 tableView.delegate = self tableView.dataSource = self searchBar.delegate = self } func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) { searchActive = true; } func searchBarTextDidEndEditing(_ searchBar: UISearchBar) { searchActive = false; } func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchActive = false; } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchActive = false; } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { filtered = items1.filter({ (text) -> Bool in let tmp: NSString = text as! NSString let range = tmp.range(of: searchText) return range.location != NSNotFound }) as! [String] if(filtered.count == 0){ searchActive = false; } else { searchActive = true; } self.tableView.reloadData() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //セクションの数返す func numberOfSections(in tableView: UITableView) -> Int { return sections.count } // セクションのタイトルを返す。 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { var title = "" for (key) in sections[section].keys { title = key } return title } //セルの数をばっこり返す//セクションごとにデータ(セル)要素数を入れるメソッド func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if(searchActive) { return filtered.count } return items1.count; } //セルを返す//表示する func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for:indexPath as IndexPath) if(searchActive){ cell.textLabel?.text = filtered[indexPath.row] } else { cell.textLabel?.text = items1[indexPath.row] as? String; } return cell; } }
回答2件
あなたの回答
tips
プレビュー