我已经为我的tableview实现了一个搜索栏:
let searchController = UISearchController(searchResultsController: nil)
并在viewDidLoad中:
override func viewDidLoad() {
super.viewDidLoad()
tblSearchTable.delegate = self
tblSearchTable.dataSource = self
loadListOfCountries()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tblSearchTable.tableHeaderView = searchController.searchBar
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
}
我的问题是,当我搜索项目时,该表显示已过滤的行,但是当我点击/单击某行时,将重新加载tableview行(所有项目)。这是我的功能:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected row")
self.dismiss(animated: false, completion: nil)
}
文本selected row
被打印但是视图控制器没有被解雇但是,就像我说的那样,重新加载tableview中的所有原始项目。
以下是扩展程序,在帮助我时可能需要它们:
@available(iOS 10.0, *)
extension Search: UISearchBarDelegate {
// MARK: - UISearchBar Delegate
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}
@available(iOS 10.0, *)
extension Search: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
以下是搜索功能:
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredArray.removeAll()
var x = 0
for a in dataArray{
if( a.lowercased().contains(searchText.lowercased())){
filteredArray.append(a)
}
x = x + 1
}
self.tblSearchTable.reloadData()
}
这里是完整的代码:
import UIKit
import CoreData
@available(iOS 10.0, *)
class Search : UIViewController, UITableViewDelegate, UITableViewDataSource{
var dataArray = [String]()
var dataLine = [String]()
var dataColor = [String]()
var filteredArray = [String]()
var filteredLine = [String]()
var filteredColor = [String]()
let searchController = UISearchController(searchResultsController: nil)
@IBOutlet var tblSearchTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tblSearchTable.delegate = self
tblSearchTable.dataSource = self
loadListOfCountries()
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tblSearchTable.tableHeaderView = searchController.searchBar
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: UITableView Delegate and Datasource functions
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected row")
self.dismiss(animated: false, completion: nil)
//var ClassViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
//ClassViewController.funcforsearch()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
return filteredArray.count
}
return dataArray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "idCell", for: indexPath as IndexPath)
if searchController.isActive && searchController.searchBar.text != "" {
cell.textLabel?.text = filteredArray[indexPath.row]
} else {
cell.textLabel?.text = dataArray[indexPath.row]
}
//cell.textLabel?.text = candy.name
//cell.detailTextLabel?.text = candy.category
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
// MARK: Custom functions
func loadListOfCountries() {
//get some array here
self.tblSearchTable.reloadData()
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
filteredArray.removeAll()
var x = 0
for a in dataArray{
if( a.lowercased().contains(searchText.lowercased())){
filteredArray.append(a)
}
x = x + 1
}
self.tblSearchTable.reloadData()
}
}
@available(iOS 10.0, *)
extension Search: UISearchBarDelegate {
// MARK: - UISearchBar Delegate
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchText: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}
@available(iOS 10.0, *)
extension Search: UISearchResultsUpdating {
public func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
谢谢!