How to create a scope search using tableview and searchbarcontroller in swift3

主宰稳场 提交于 2019-12-02 13:34:57

问题


Hello all have designed a scope search using tableview and serchbarcontroller. To achieve this have used below code but somehow its not returning me the actual output. Hoping for the help. Thank you.

output :

here is my output's ScreenShot

code :

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")
}

}

回答1:


Can you try it this way:

  1. Make an array of auto-completion text-field, one for each of the data source.
  2. Let these text-fields to be invisible at first.
  3. After you select values and click the search button, let the corresponding text-fields to be visible.

Not sure if it will meet your requirement, please leave comments if you have any problem.



来源:https://stackoverflow.com/questions/44985996/how-to-create-a-scope-search-using-tableview-and-searchbarcontroller-in-swift3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!