Uitableview 如何在swift3中使用tableview和searchbarcontroller创建范围搜索

Uitableview 如何在swift3中使用tableview和searchbarcontroller创建范围搜索,uitableview,swift3,uisearchbardelegate,Uitableview,Swift3,Uisearchbardelegate,Hello all使用tableview和serchbarcontroller设计了一个范围搜索。为了实现这一点,我使用了下面的代码,但不知怎么的,它没有返回我的实际输出。希望得到帮助。多谢各位 输出: 代码: import UIKit class SearchBookVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate {

Hello all使用tableview和serchbarcontroller设计了一个范围搜索。为了实现这一点,我使用了下面的代码,但不知怎么的,它没有返回我的实际输出。希望得到帮助。多谢各位

输出:

代码:

import UIKit

class SearchBookVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate {

@IBOutlet weak var tableview: UITableView!

struct Books  {
    var name = String()
    var board = String()
    var classname = String()
    var area = String()
    var city = String()
    }

    var books = [Books(name: "Physics", board: "CBSE",classname:"XI",area:"Karnataka",city:"Bangalore"),
                 Books(name:"English",board:"CBSE",classname:"X",area:"Maharashtra",city:"pune"),
                 Books(name:"biology",board:"IB",classname:"XII",area:"Gujarat",city:"Rajkot"),

                 Books(name:"chemistry",board:"IB",classname:"X",area:"Gujarat",city:"Ahmedabad"),

                 Books(name:"Maths",board:"ICSE",classname:"IX",area:"Maharashtra",city:"Mumbai"),
                 Books(name:"Science",board:"ICSE",classname:"XII",area:"Karnataka",city:"Mysore")
                 ]
    var filteredBooks = [Books]()

    let searchController = UISearchController(searchResultsController: nil)



    override func viewDidLoad() {
    super.viewDidLoad()

      filteredBooks = books

       searchController.searchResultsUpdater = self
       searchController.dimsBackgroundDuringPresentation = false
       definesPresentationContext = true
       tableview.tableHeaderView = searchController.searchBar

       searchController.searchBar.scopeButtonTitles = ["All","Name", "Board", "Class", "Area","City"]

       searchController.searchBar.delegate = self

       self.tableview.register(mycell.self, forCellReuseIdentifier: "cell")


    // Do any additional setup after loading the view.
}

func applySearch(searchText: String, scope: String = "All") {


    if searchController.searchBar.text! == ""
    {
        filteredBooks = books.filter { book in
            let nameofbook = ( scope == "All") || (book.name == scope) || (book.board == scope) || (book.classname == scope) || (book.area == scope) || (book.city == scope)
            return nameofbook
    }
    }
        else
        {
            filteredBooks = books.filter { book in
                let nameofbook = ( scope == "All") || (book.name == scope) || (book.board == scope) || (book.classname == scope) || (book.area == scope) || (book.city == scope)
                return nameofbook && book.name.lowercased().contains(searchText.lowercased()) && book.board.lowercased().contains(searchText.lowercased()) && book.classname.lowercased().contains(searchText.lowercased()) && book.area.lowercased().contains(searchText.lowercased()) && book.city.lowercased().contains(searchText.lowercased())
            }
        }
    self.tableview.reloadData()
}
func updateSearchResults(for searchController: UISearchController) {

     let searchBar = searchController.searchBar
    let selectedScope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
    applySearch(searchText: searchController.searchBar.text!,scope: selectedScope)
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    applySearch(searchText: searchController.searchBar.text!,scope: searchBar.scopeButtonTitles![selectedScope])
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.filteredBooks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! mycell

    cell.bookname?.text = self.filteredBooks[indexPath.row].name
    cell.Boardname?.text = self.filteredBooks[indexPath.row].board
    cell.classtitle?.text = self.filteredBooks[indexPath.row].classname

    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Row \(indexPath.row) selected")
}

}

你能这样试试吗

  • 创建一个自动完成文本字段数组,每个字段对应一个数据源
  • 首先让这些文本字段不可见
  • 选择值并单击搜索按钮后,让相应的文本字段可见

  • 不确定它是否符合您的要求,如果您有任何问题,请留下意见。

    您可以这样尝试吗:

  • 创建一个自动完成文本字段数组,每个字段对应一个数据源
  • 首先让这些文本字段不可见
  • 选择值并单击搜索按钮后,让相应的文本字段可见

  • 不确定是否符合您的要求,如果您有任何问题,请留下评论。

    对不起,我不太理解您的情况。您的意思是要使用您选择的单词作为自动完成文本字段的数据源吗?自动完成的源表示我们提供给文本字段的值稍后将来自API。例如,所有国家的名称都来自API。因此,您希望通过选择数据源(如学校、城市等)生成自动完成字段?是,并希望在点击搜索按钮时根据所有选择的值显示数据。您现在被困在哪里,请发布您的一些代码?对不起,我真的不明白你的情况。您的意思是要使用您选择的单词作为自动完成文本字段的数据源吗?自动完成的源表示我们提供给文本字段的值稍后将来自API。例如,所有国家的名称都来自API。因此,您希望通过选择数据源(如学校、城市等)来生成自动完成字段?是,并希望在点击搜索按钮时根据所有选择的值显示数据。您现在在哪里,你能发布一些你的代码吗?谢谢你的帮助。但是如果你能帮助我,请参考我更新的问题。我已经更改了我的设计,并用当前的设计和代码更新了我的问题。谢谢你的帮助。但是如果你能帮我,请参考我更新的问题。我已经用当前的设计和代码更改了我的设计并更新了我的问题代码。