user6393953
user6393953

Reputation:

can't see navigation anymore with searchbar

I'm building an app in which one of the functions will be sorting all of the shops in the main shopping street of our town. This is why I made a table view with some shops listed inside, with a category and a name.

I sorted them alphabetically, and made a search bar for it as you can see in this video. Something goes wrong with the navigation though, it works when not using the search bar, but it disappears when using it.

Can anybody help me with this? Thanks in advance! The code for the main view controller (which contains the complete navigation, since nothing about it's done in the storyboard) is mentioned below:

import UIKit

class MasterTableViewController: UITableViewController {

//winkels filteren
var filteredWinkels = [Winkel]()

// MARK: - Properties
var detailViewController: DetailViewController? = nil
var winkels = [Winkel]()

// MARK: - View Setup
override func viewDidLoad() {
    super.viewDidLoad()

    winkels = [
        Winkel(category:"Literature", name:"Standaard"),
        Winkel(category:"Literature", name:"Acco"),
        Winkel(category:"Clothing", name:"H&M"),
        Winkel(category:"Clothing", name:"C&A"),
        Winkel(category:"Clothing", name:"Patio"),
        Winkel(category:"Restaurants", name:"De 46"),
        Winkel(category:"Cafés", name:"'t Hoekske"),
        Winkel(category:"Supermarkets", name:"Carrefour"),
        Winkel(category:"Supermarkets", name:"Colruyt"),
        Winkel(category:"Supermarkets", name:"Blokker"),
        Winkel(category:"Lingerie", name:"Hunkemoller")
    ]

    //alfabetisch sorteren
    winkels.sortInPlace({ $0.name < $1.name })

    if let splitViewController = splitViewController {
        let controllers = splitViewController.viewControllers
        detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? DetailViewController
    }

    //searchController aanmaken
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    //searchButtons aanmaken
    searchController.searchBar.scopeButtonTitles = ["All", "Clothing", "Supermarkets", "Literature"]
    searchController.searchBar.delegate = self


    self.tableView.reloadData()
}

override func viewWillAppear(animated: Bool) {
    clearsSelectionOnViewWillAppear = splitViewController!.collapsed
    super.viewWillAppear(animated)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

//aantallen tellen
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchController.active && searchController.searchBar.text != "" {
        return filteredWinkels.count
    }
    return winkels.count
}

//naam cel aanpassen en checken
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let winkel: Winkel

    if searchController.active && searchController.searchBar.text != "" {
        winkel = filteredWinkels[indexPath.row]
    } else {
        winkel = winkels[indexPath.row]
    }

    if winkel.name .containsString("Hunkemoller") {
        cell.textLabel!.text = "Hunkemöller"
    } else {
        cell.textLabel!.text = winkel.name
    }
    cell.detailTextLabel?.text = winkel.category
    return cell
}

// Segues voorbereiden
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let winkel: Winkel
            if searchController.active && searchController.searchBar.text != "" {
                winkel = filteredWinkels[indexPath.row]
            } else {
                winkel = winkels[indexPath.row]
            }

            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailWinkel = winkel
            controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("showDetail", sender: self)
}

//searchcontroller aanmaken
let searchController = UISearchController(searchResultsController: nil)

//scopebar maken
func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredWinkels = winkels.filter { winkel in
        let categoryMatch = (scope == "All") || (winkel.category == scope)
        return  categoryMatch && winkel.name.lowercaseString.containsString(searchText.lowercaseString)
    }

    tableView.reloadData()
}

}

// updaten
extension MasterTableViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
    let searchBar = searchController.searchBar
    let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
    filterContentForSearchText(searchController.searchBar.text!, scope: scope)
}
}

extension MasterTableViewController: UISearchBarDelegate {
func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
    filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}
}

Upvotes: 0

Views: 61

Answers (2)

J.Hunter
J.Hunter

Reputation: 586

UISearchController has a property named hidesNavigationBarDuringPresentation, default is YES. I don't make sure if it is the root cause.

You can implement - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath of UITableViewDelegate, use self.navigationController.pushViewController to push your DetailViewController instead of using segue

Upvotes: 2

Alex Kosyakov
Alex Kosyakov

Reputation: 2176

You can set your navigation bar status of destination view controller in your prepareForSegue: method:

let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailWinkel = winkel
controller.navigationBarHidden = true

Upvotes: 0

Related Questions