如何使用搜索控制器更新表控制器过滤?

时间:2017-02-07 11:48:28

标签: ios swift uitableview uisearchcontroller

我是IOS开发的新手,因此很难理解一些事情。

在我的应用中,当用户打开主屏幕时,我会显示一个任务列表和一个搜索来过滤任务。如果用户在我的搜索控制器中键入“星期一”,它应该只过滤星期一的任务。所有代码都运行正常,但我不知道它是否正确。

我有一个表视图控制器,它包含用户打开应用程序时的所有任务和显示。因此,用户应该首先看到的是他的所有任务。

当用户在表头中打开搜索时,它应该过滤他的任务(来自第一个表)并仅显示与过滤器对应的任务。它也正常工作。

现在,我的问题。我有两张桌子。一个用于主视图控制器,另一个用于搜索控制器。 我必须为获取单元格编写两次代码并编辑删除任务的样式。

我做错了什么?如何在主表中使用过滤器并将所有内容保存在主表中,而不是将所有内容复制到第二个控制器?

由于

2 个答案:

答案 0 :(得分:1)

首先,不需要使用两个表进行搜索和显示,这根本不是一个好方法。您还应该在Search Controller上查找Apple文档。您应该使用搜索栏和搜索显示控制器,并在其viewDidLoad方法

中实现以下代码
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

以下方法基于searchText过滤主阵列,并将结果放入filteredData数组中。

func filterContentForSearchText(searchText: String, scope: String = "All") {
     filteredData = array.filter { string in
     return     
     string.lowercaseString.containsString(searchText.lowercaseString)
  }

   tableView.reloadData()
}

要允许ViewController响应搜索栏,它必须实现UISearchResultsUpdating。打开ViewController.swift并在主ViewController类之外添加以下类扩展:

extension MasterViewController: UISearchResultsUpdating {
  func updateSearchResultsForSearchController(searchController: UISearchController) {
    filterContentForSearchText(searchController.searchBar.text!)
  }
}

现在在您的numberOfRowsInSection和cellForRowAtIndexPath方法中执行以下操作

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      if searchController.active && searchController.searchBar.text != "" {
    return filteredData.count
  }
  return array.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
  let string: String
  if searchController.active && searchController.searchBar.text != "" {
string = filteredData[indexPath.row]
  } else {
      string = array[indexPath.row]
   }
  cell.textLabel?.text = string
  return cell
}

有关详细信息,请访问以下网站。 https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

答案 1 :(得分:0)

取一个Bool var searchActive = false和一个空数组var filteredItems = [NSDictionary]() 你的searchBarDelegate就像是 // MARK: - SearchBar Delegate

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    searchActive = true
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
    searchActive = false
    resignFirstResponder()
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchActive = false
}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchActive = false
    resignFirstResponder()
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    filteredItems = itemsArray.filter{
      let stringa = $0["name"] as! String!
        let tmp: NSString = stringa
        let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound
    }
    if(filteredItems.count == 0){
        searchActive = false
    } else {
        searchActive = true
    }
    mTableView.reloadData()

}

和主tableView委托将

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchActive == true{
            return filteredItems.count

        }
        else{
            return itemsArray.count
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("XXXXX") as! TableViewCellVC
      let item:NSDictionary!
        if searchActive == false {
           item = itemsArray[indexPath.row]
         }else{
          item = filteredItems[indexPath.row]
        }

        return cell
    }