搜索条控制器在swift中

时间:2017-07-24 10:44:36

标签: ios swift swift3 uisearchbar

我是swift编程的新手

tableview标题中的搜索栏控制器。我可以手动给它的搜索名称工作正常,但在我的情况下,我必须在搜索栏控制器中给出名称并显示学生的名字。搜索栏控制器使用搜索文本使用过滤器选项。

这是代码

Thread group 1-6

使用过滤器

将文本搜索到控制器

enter image description here

请帮帮我

2 个答案:

答案 0 :(得分:1)

从问题我相信您正在正确执行搜索,但您想获得在searchBar中输入的单词/文字来执行搜索。 我们将使用searchBar.text

UISearchBarController属性获取在searchBar中输入的文字

尝试

let searchToSearch = searchController.searchBar.text

答案 1 :(得分:0)

您可以使用UISearchBar的shouldChangeTextIn Delegate方法。

  func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    let newCharLength = (searchBar.text ?? "").characters.count + text.characters.count - range.length

    if newCharLength.count > 0 {

        arrSearchFilter = arrNames.filter { (str) -> Bool in
                    return (str.lowercased().contains((searchBar.text?.lowercased())!))!
                }

             if arrSearchLocalityModel.count > 0 {
                    arrSearchLocality = arrSearchLocalityModel
                } else {
                    arrSearchLocality = arrLocalityModel
                }

          self.tableView.reloadData()

        }


    }