gpt4 book ai didi

ios - 搜索栏看不到导航了

转载 作者:行者123 更新时间:2023-11-28 12:47:18 25 4
gpt4 key购买 nike

我正在构建一个应用程序,其中一个功能是对我们城镇主要购物街上的所有商店进行分类。这就是为什么我做了一个表格 View ,里面列出了一些商店,有一个类别和一个名称。

我按字母顺序对它们进行了排序,并为其创建了一个搜索栏,正如您在这个 video 中看到的那样.虽然导航出了点问题,但它在不使用搜索栏时有效,但在使用时消失了。

谁能帮我解决这个问题?提前致谢!主视图 Controller 的代码(包含完整的导航,因为在 Storyboard中没有做任何事情)如下所示:

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

最佳答案

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

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

关于ios - 搜索栏看不到导航了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com