У меня есть приложение tableView с searchBar, показывающее массив со списком продуктов. После того, как поиск сделан, пользователь нажимает на бакалейную лавку и отправляет второй просмотр таблицы. Это нормально работает, проблема заключается в следующем:
В следующем (втором) представлении таблицы с информацией о продуктовом поискеBar все еще там и с клавиатурой.
Как я могу отклонить клавиатуру и поиск, которые не должны появляться во втором представлении таблицы. Любая помощь более чем приветствуется :)
Панель поиска в виде таблицы
// DictionaryTableViewController.swift
import UIKit
class DictionaryTableViewController: UITableViewController, UISearchResultsUpdating
{
var searchController:UISearchController!
var searchResults:[Dictionary] = []
var dictionaries:[Dictionary] =[
Dictionary (word: "grocery1", definition: "this is the grocery1 definition"),
Dictionary (word: "grocery2", definition: "this is the grocery2 definition"),
Dictionary (word: "grocery3", definition: "this is the grocery3 definition"),
Dictionary (word: "grocery4", definition: "this is the grocery4 definition"),
]
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.active {
return searchResults.count
} else {
return dictionaries.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DictionaryTableViewCell
let dictionary = (searchController.active) ? searchResults[indexPath.row]: dictionaries[indexPath.row]
// Configure the cell...
cell.wordLabel.text = dictionary.word
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDictionaryDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationController = segue.destinationViewController as! DictionaryDetailViewController
destinationController.dictionary = (searchController.active) ? searchResults[indexPath.row] : dictionaries[indexPath.row]
}
}
}
func updateSearchResultsForSearchController(searchController:
UISearchController) {
if let searchText = searchController.searchBar.text {
filterContentForSearchText(searchText)
tableView.reloadData()
}
}
func filterContentForSearchText(searchText: String) {
searchResults = dictionaries.filter({ (dictionary:Dictionary) -> Bool in
let wordMatch = dictionary.word.rangeOfString(searchText, options:
NSStringCompareOptions.CaseInsensitiveSearch)
return wordMatch != nil
})}}
только что отредактировал мой вопрос и ввел код, который я следую. Извините за недостаток информации раньше. – Manolo
Спасибо, что Ибрагим работает очень хорошо. :) – Manolo