【问题标题】:Swift - Dismiss search controllerSwift - 关闭搜索控制器
【发布时间】:2016-08-04 19:43:34
【问题描述】:

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

流程是这样的

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

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

所以基本上我有一些问题:
1.- 我的方法对 iOS 友好吗?也许我找不到关于我如何努力实现的资源,因为它不是。
2.- 如果为真,按下键盘上的搜索按钮后如何关闭搜索控制器?
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


【解决方案1】:

假设您使用的是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()
}

【讨论】:

  • 感谢您的回答,但这假设我想在表格上显示一个搜索栏,并在那里触发搜索功能。在我的例子中,搜索是通过在包含 tableview 的导航控制器上按下 UIBarButtonItem 来触发的。
  • 好吧,接受你的回答,我终于设法做到了,我只需要稍作调整以适应我的情况。非常感谢!
【解决方案2】:

接受@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(_:))
}

【讨论】:

    【解决方案3】:
    self.searchController.dismissViewControllerAnimated(true, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-13
      • 2016-05-12
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      相关资源
      最近更新 更多