搜索结果可以快速搜索到其他View控制器

时间:2018-09-15 18:16:31

标签: ios swift uisearchresultscontroller

我正在导航栏上以编程方式实现搜索控制器,它工作正常并显示结果,如果我可以选择tableview单元格(选择方法),segue则工作正常。当我选择搜索结果时,它并不会连接到下一个视图控制器。它显示了相同的视图控制器,并且tableview正在重新加载数据。我已经显示一次单击搜索结果就可以连接到下一个视图控制器

image enter image description here

var modeldata = [ModelData]()
var FilerData = [ModelData]() 


override func viewDidLoad() {
        super.viewDidLoad()

        self.searchcontroller = UISearchController(searchResultsController: nil)
        self.searchcontroller.delegate = self
        self.searchcontroller.searchBar.delegate = self
        self.searchcontroller.hidesNavigationBarDuringPresentation = false
        self.searchcontroller.dimsBackgroundDuringPresentation = true
        self.navigationItem.titleView = searchcontroller.searchBar
        self.definesPresentationContext = true
        self.searchcontroller.searchResultsUpdater = self
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modeldata.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
        let row = indexPath.row
        let values = modeldata[row] as ModelData

        cell.NameLabel.text = values.name 
return cell
}

func updateSearchResults(for searchController: UISearchController) {

        let searchToSearch = searchController.searchBar.text
        if(searchToSearch == "")
        {
            modeldata = self.FilerData
        }
        else{
            modeldata.removeAll()
            let itemsarray = self.FilerData
            var ListArray = [String]()
            for ListItems in itemsarray {
                ListArray.append(ListItems.name!)
                if(ListItems.name?.range(of: searchToSearch!, options: .caseInsensitive) != nil)
                {
                    self.modeldata.append(ListItems)

                }
            }
        }
        self.TableView.reloadData()
      }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        performSegue(withIdentifier: "next", sender:indexPath.row )

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let selectrow = sender as? Int
        let name = modeldata[selectrow!].name
        let nextview = segue.destination as? WekiViewController
        nextview?.namestring = name!
    }

0 个答案:

没有答案