jueves, 6 de abril de 2017

iOS: Search view

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {

@IBOutlet var tableView: UITableView! // Add in interface builder

var items = [Item]()
    var filteredItems = [Item]()
    var searchController: UISearchController!

override func viewDidLoad() {
        super.viewDidLoad()
       
        ...
       
        self.searchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.hidesNavigationBarDuringPresentation = false
            controller.searchBar.sizeToFit()
            //            controller.searchBar.barStyle = UIBarStyle.black
            controller.searchBar.barTintColor = settings.get(settingNamed: SettingCollection.General.PrimaryButtonBackgroundColor)?.colorValue
            controller.searchBar.backgroundColor = settings.get(settingNamed: SettingCollection.General.PrimaryButtonBackgroundColor)?.colorValue
            self.tableView.tableHeaderView = controller.searchBar
            return controller
        })()
    }
   
    ...
   
    // MARK: - UITableViewDelegate & UITableViewDataSource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredItems.count
    }
   
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        let item = filteredItems[indexPath.row]
       
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCell
        // Populate cell
       
        return cell
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
       
    }
   
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
   
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
            cell.separatorInset = UIEdgeInsets.zero
        }
        if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
            cell.preservesSuperviewLayoutMargins = false
        }
        if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
            cell.layoutMargins = UIEdgeInsets.zero
        }
    }
   
    // MARK: - UISearchResultsUpdating
    func updateSearchResults(for searchController: UISearchController) {
        filterContentForSearchText(searchText: searchController.searchBar.text!)
    }
   
    func filterContentForSearchText(searchText: String, scope: String = "All") {
       
        if searchText.isEmpty {
            filteredItems = items
            tableView.reloadData()
           
            return
        }
       
        filteredItems = items.filter { item in
            return item.text.lowercased().contains(searchText.lowercased())
        }
       
        tableView.reloadData()
    }
}

No hay comentarios:

Publicar un comentario