Bir dizim var ve gerçek zamanlı arama yapmaya çalışıyorum. Yani orijinal olarak filtrelenmemiş diziye sahip olacağım ve tabloyu ararken arama yaptığım gibi güncellenecektir. Delegeyi belirledim, bu yüzden neden çalışmadığı konusunda kayboldum. Herhangi bir yardım takdir :)Arama neden çalışmıyor?
@IBOutlet weak var table: UITableView!
var searchController = UISearchController()
var unfilitered = ["cat", "dog", "bat", "tiger"]
var filtered = [String]()
var shouldShowSearchResults = false
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: searchcell = table.dequeueReusableCellWithIdentifier("CELL") as! searchcell
if shouldShowSearchResults {
cell.animal.text = filtered[indexPath.row]
}
else {
cell.animal.text = unfiltered[indexPath.row]
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filtered.count
}
else {
return unfiltered.count
}
}
func configureSearchController() {
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.placeholder = "Search"
searchController.searchBar.sizeToFit()
table.tableHeaderView = searchController.searchBar
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
shouldShowSearchResults = true
table.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
shouldShowSearchResults = false
table.reloadData()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
if !shouldShowSearchResults {
shouldShowSearchResults = true
table.reloadData()
}
searchController.searchBar.resignFirstResponder()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = searchController.searchBar.text
filtered = unfiltered.filter({ (animal) -> Bool in
let animalText: NSString = animal
return (animalText.rangeOfString(searchString!, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
})
table.reloadData()
}
'Filtrele' ne istediğinizi içerdiğini doğruladınız mı? UpdateSearchResultsForSearchController' çağrılıyor mu? – Michael
Birkaç kesme noktası ekledim ve işaretledim ve DidBeginEditing çağrılmıyor. – user6032625
"UITextField" için temsilci ayarlamadığınız gibi görünüyor. 'UpdateSearchResultsForSearchController' içinde 'shouldShowSearchResults' ayarlayabilirdiniz. – Michael