用搜索栏看不到导航了

时间:2016-06-04 22:31:40

标签: ios swift uisearchcontroller

我正在建立一个应用程序,其中一个功能将整理我们镇主要购物街的所有商店。这就是为什么我制作了一个桌面视图,其中列出了一些商店,其中包含类别和名称。

我按字母顺序对它们进行了排序,并在此video中为您创建了搜索栏。但导航出了问题,它在不使用搜索栏时有效,但在使用时会消失。

有人可以帮我这个吗?提前致谢!主视图控制器的代码(包含完整的导航,因为它没有在故事板中完成),如下所述:

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

2 个答案:

答案 0 :(得分:2)

UISearchController有一个名为hidesNavigationBarDuringPresentation的属性,默认为YES。我不确定它是否是根本原因。

您可以实施- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath UITableViewDelegate,使用self.navigationController.pushViewController推送您的DetailViewController而不是使用segue

答案 1 :(得分:0)

您可以在prepareForSegue:method中设置目标视图控制器的导航栏状态:

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