// 自定义cell
class Cell: UITableViewCell {
@IBOutlet weak var fenLei: UILabel!
@IBOutlet weak var tangGuoMing: UILabel!
var mode: Candy!
{
didSet {
updateUI()
}
}
fileprivate func updateUI()
{
fenLei.text = mode.name
tangGuoMing.text = mode.category
}
}
// prepare
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! UINavigationController
if let index = myTableView.indexPathForSelectedRow {
let candy: Candy
if searchController.isActive {
candy = filterCandies[(index.row)]
} else {
candy = arrMode[(index.row)]
}
let detail = vc.topViewController as! ViewController
detail.mode = candy
}
}
// searchController初始化
func setSearchController() -> Void {
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
myTableView.tableHeaderView = searchController.searchBar
searchController.searchBar.scopeButtonTitles = ["All", "Chocolate", "Hard", "Other"]
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
}
func updateSearchResults(for searchController: UISearchController) {
let searchBar = searchController.searchBar
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(text: searchBar.text!, scope: scope)
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(text: searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}