UITableViewにRealmで取得したデータを、UISearchControllerを使用してフィルターをかけたデータ一覧を表示したいのですが、シュミレーター上でSearchBarをタップしても入力欄に文字を入力出来ずに閉じてしまいます。
原因がわからないので、どこがいけないのかアドバイスを頂きたいです。
###該当ソース
Realmで取得したデータ(Words)をArrayに変換してwordListに格納しています。
SearchBarで入力した文字でfilterした配列をfilteredWordsに格納してtableViewに表示しようとしていますが、入力文字が空でもfilteredWordsに格納できていないです。
model
1class Words{ 2 @objc dynamic var wordId: String? = NSUUID().uuidString 3 @objc dynamic var word: String? = "" 4 @objc dynamic var categoryId: Int = 0 5 @objc dynamic var categoryName: String? = "" 6 let category = LinkingObjects(fromType: Category.self, property: "words") 7 8 @objc dynamic var userId: String? = "" 9 @objc dynamic var ideaFlag: Int = 0 10 11 override class func primaryKey() -> String { 12 return "wordId" 13 } 14}
ViewController
1//抜粋 2 3 //display tableview header 4 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 5 let headerview = UIView() 6 headerview.backgroundColor = UIColor.white 7 8 let cell = tableView.dequeueReusableCell(withIdentifier:"WordItemCell") as! WordTableViewCell 9 cell.delegate = self 10 cell.dropdown.dataSource = arrayCategoryList() 11 12 searchController = UISearchController(searchResultsController: nil) 13 searchController.searchResultsUpdater = self 14 searchController.searchBar.sizeToFit() 15 searchController.dimsBackgroundDuringPresentation = false 16 searchController.hidesNavigationBarDuringPresentation = false 17 searchController.searchBar.placeholder = "Search" 18 self.definesPresentationContext = true 19 20 if #available(iOS 11.0, *) { 21 headerview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 45) 22 cell.frame = CGRect(x:0, y:0, width:self.view.frame.size.width, height:44) 23 self.navigationItem.searchController = searchController 24 } else { 25 headerview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 100) 26 cell.frame = CGRect(x:0, y:55, width:self.view.frame.size.width, height:44) 27 searchController.searchBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50) 28 headerview.addSubview(searchController.searchBar) 29 } 30 31 headerview.addSubview(cell) 32 33 return headerview 34 } 35 36//SearchController SearchResultUpdating 37extension ViewController: UISearchResultsUpdating{ 38 func updateSearchResults(for searchController: UISearchController) { 39 filterContentForSearchText(searchController.searchBar.text!) 40 } 41 42 func searchBarIsEmpty() -> Bool { 43 // Returns true if the text is empty or nil 44 return searchController.searchBar.text?.isEmpty ?? true 45 } 46 47 func filterContentForSearchText(_ searchText: String) { 48 //wordList:[Words] = Array(realm.object(Words.self)) 49 //filteredWords = [Words]() 50 filteredWords = wordList.filter({( words : Words) -> Bool in 51 return words.word!.lowercased().contains(searchText.lowercased()) 52 }) 53 tableView.reloadData() 54 } 55} 56
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 07:25
2018/11/08 15:01