Swift - 关闭搜索控制器

问题描述 投票:0回答:3

我目前正在尝试使用从API调用中检索到的动态数据在UITableView上实现搜索功能。

流程将是这样的

  1. 按右栏按钮项(搜索功能)
  2. 在表视图上显示搜索控制器。
  3. 用户在其上输入一些文本并按下搜索按钮
  4. 控制器根据用户的搜索执行API调用。
  5. 表视图将填充结果。

我已经看到了关于如何在tableView上实现搜索的sokme示例,我认为我的不同,因为我手动呈现搜索控制器。在按下搜索按钮之前不会显示它(作为示例的opossite,当UISearch被添加到表头视图并且当用户点击搜索栏时触发搜索功能。)

所以基本上我有一些问题: 1.-我的方法是iOS友好的吗?也许我找不到有关我如何实现的资源,因为事实并非如此。 2.-如果为true,如何在按下键盘上的搜索按钮后关闭搜索控制器? 3.-如何区分键盘上的搜索按钮或搜索栏上的取消按钮触发的解雇?

到目前为止,这是我的代码:

extension MyTableViewController: UISearchResultsUpdating {
  func updateSearchResultsForSearchController(searchController: UISearchController) {
  }
}

class MyTableViewController: UITableViewController, UISearchControllerDelegate {
  var searchController: UISearchController!
  var data = [CoolData]()

  override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "search"), style: .Plain, target: self, action: .SearchTapped)
  }

  func searchButtonTapped(sender: UIBarButtonItem) {
    searchController = UISearchController(searchResultsController: nil)
    definesPresentationContext = true
    searchController.searchResultsUpdater = self
    searchController.delegate = self
    searchController.searchBar.translucent = false
    searchController.searchBar.barTintColor = UIColor(hexString: BACConstants.color.Yellow)
    searchController.searchBar.tintColor = UIColor(hexString: BACConstants.color.Black)
    self.presentViewController(searchController, animated: true, completion: nil)
  }

}

private extension Selector {
  static let SearchTapped = #selector(InvolvedProcessesViewController.searchButtonTapped(_:))
}
ios swift uitableview search uisearchcontroller
3个回答
2
投票

假设您正在使用UITableViewController,您可以像这样添加UISearchController

var shouldShowSearchResults = false
var searchController: UISearchController!

func configureSearchController() {
    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()

    tableView.tableHeaderView = searchController.searchBar
}

然后,实现这两个代表:UISearchResultsUpdatingUISearchBarDelegate

 // MARK: - UISearchBarDelegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    // do your thing
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    // do your thing
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    // do your thing
    searchController.searchBar.resignFirstResponder()
}

// MARK: - UISearchResultsUpdating

func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchString = searchController.searchBar.text!.lowercaseString

    // do your thing..

    tableView.reloadData()
}

1
投票

以@ matias-elorriaga的回答我终于成功了。我使用错误的委托来处理我的搜索事件。

extension MyTableViewController: UISearchResultsUpdating {
  func updateSearchResultsForSearchController(searchController: UISearchController) {
  }
}

class MyTableViewController: UITableViewController, UISearchBarDelegate {
  var searchController: UISearchController!
  var data = [CoolData]()

  override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "search"), style: .Plain, target: self, action: .SearchTapped)
  }

  func searchButtonTapped(sender: UIBarButtonItem) {
    searchController = UISearchController(searchResultsController: nil)
    definesPresentationContext = true
    searchController.searchResultsUpdater = self
    searchController.delegate = self
    searchController.searchBar.translucent = false
    searchController.searchBar.barTintColor = UIColor(hexString: BACConstants.color.Yellow)
    searchController.searchBar.tintColor = UIColor(hexString: BACConstants.color.Black)
    self.presentViewController(searchController, animated: true, completion: nil)
  }    

  func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    // Do stuff
  }

  func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    // Do stuff
  }

  func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    self.searchController.dismissViewControllerAnimated(true, completion: nil) // When search is tapped, dismiss the search and fetch the results based on the filter 
  }
}

private extension Selector {
  static let SearchTapped = #selector(InvolvedProcessesViewController.searchButtonTapped(_:))
}

0
投票
self.searchController.dismissViewControllerAnimated(true, completion: nil)
© www.soinside.com 2019 - 2024. All rights reserved.