Swift - 关闭搜索控制器

时间:2016-08-04 19:43:34

标签: ios swift uitableview search uisearchcontroller

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

流程将是这样的

  1. 按右栏按钮项(搜索功能)
  2. 搜索控制器呈现在表格视图上。
  3. 用户在其上输入一些文字并按下搜索按钮
  4. 控制器根据用户的搜索执行API调用。
  5. 表格视图将填充结果。
  6. 我已经看过如何通过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(_:))
    }
    

3 个答案:

答案 0 :(得分: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 :(得分: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(_:))
}

答案 2 :(得分:0)

self.searchController.dismissViewControllerAnimated(true, completion: nil)