UITableView のデータを検索したかったので、UISearchController を付けてフィルター機能を付けた。
import UIKit
class ViewController: UIViewController{
@IBOutlet weak var tableView: UITableView!
var data1 = [String]()
var searchResults = [String]()
let sectionName = ["data"]
var searchController:UISearchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.tableFooterView = UIView(frame: .zero)
tableView.allowsSelectionDuringEditing = true
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
mySearchController.hidesNavigationBarDuringPresentation = true //検索バーを押したらナビゲーションバーを隠すか
mySearchController.dimsBackgroundDuringPresentation = true //検索中は後ろを暗くするか
//searchController.isActive = true
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.hidesSearchBarWhenScrolling = true
navigationItem.searchController = searchController
self.data1 = 【"text1","text2","text12"】
self.tableView.reloadData()
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if( searchController.searchBar.text != "" ) {
return self.searchResults.count
} else {
return self.data1.count
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionName.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionName[section]
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if( searchController.searchBar.text != "" ) {
cell.textLabel?.text = self.searchResults[indexPath.row].title
} else {
cell.textLabel?.text = self.data1[indexPath.row].title
}
return cell
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if( searchController.searchBar.text != "" ) {
print(self.searchResults[indexPath.row])
}else{
print(self.data1[indexPath.row])
}
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
}
}
extension ViewController: UISearchResultsUpdating{
func updateSearchResults(for searchController: UISearchController) {
self.searchResults = self.data1.filter { data in
return data.title.contains(searchController.searchBar.text!)
}
self.tableView.reloadData()
}
}