[英]Swift Navigationbar searchbar dismiss search text on click
當我在搜索欄中搜索時,它會相應地給我結果,但是當我在外部單擊以選擇選項時,搜索欄占位符將變為nil,只有搜索到的選項可用。 要獲取所有選項,我必須再次打開搜索欄,然后按“取消”,這將重新加載表格視圖以顯示所有選項。
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UISearchControllerDelegate,UISearchBarDelegate {
let locationNames = ["Python", "C#", "Machine Learning"]
let locationImages = [UIImage(named: "hawaiiResort"), UIImage(named: "mountainExpedition"), UIImage(named: "scubaDiving")]
let locationDescription = ["Beautiful resort off the coast of Hawaii", "Exhilarating mountainous expedition through Yosemite National Park", "Awesome Scuba Diving adventure in the Gulf of Mexico"]
let searchController = UISearchController(searchResultsController: nil)
@IBOutlet weak var collectionView: UICollectionView!
var searchResult = [String]()
var searching = false
override func viewDidLoad() {
navigationItem.title = "Courses"
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
navigationItem.searchController = searchController
definesPresentationContext = true
currentPage = 0
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(searching){
return searchResult.count
}
else{
return 3
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
if(searching)
{
cell.locationName.text = searchResult[indexPath.row]
let people = locationNames.index{$0 == String(searchResult[indexPath.row])}
cell.locationImage.image = locationImages[people ?? 0]
cell.locationDescription.text = locationDescription[people ?? 0]
}
else
{
cell.locationImage.image = locationImages[indexPath.row]
cell.locationName.text = locationNames[indexPath.row]
cell.locationDescription.text = locationDescription[indexPath.row]
}
cell.contentView.layer.cornerRadius = 4.0
cell.contentView.layer.borderWidth = 1.0
cell.contentView.layer.borderColor = UIColor.clear.cgColor
cell.contentView.layer.masksToBounds = false
cell.layer.shadowColor = UIColor.gray.cgColor
cell.layer.shadowOffset = CGSize(width: 0, height: 1.0)
cell.layer.shadowRadius = 4.0
cell.layer.shadowOpacity = 1.0
cell.layer.masksToBounds = false
cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
vc.selectedCourse = locationNames[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension ViewController: UISearchResultsUpdating{
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
searchResult = locationNames.filter({$0.prefix(searchText.count) == searchText})
searching = true
collectionView.reloadData()
}
// func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// searchResult = locationNames.filter({$0.prefix(searchText.count) == searchText})
// searching = true
// collectionView.reloadData()
// }
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
NSLog("%@",searchBar.text ?? "");
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
collectionView.reloadData()
}
}
我是Swift的新手。
首先,您應該讓updateSearchResults(for:)
使過濾/更新工作,這是它的唯一目的。
對於答案,也許您是在將searchBar
外部點擊時將UISearchController
的isActive
方法設置為false
,從而導致其中的文本被刪除,並且不允許搜索控制器進行相應更新以重新加載所有數據。
因此,一個解決方案可以是將所述searchBarTextDidEndEditing(_)
方法中,當重置數據源searchBar
不再是對第一響應者:
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searching = false
searchResult = locationNames
collectionView.reloadData()
}
甚至更好的是,讓updateSearchResults(for:)
方法自動更新數據:
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
searchResult = locationNames.filter({$0.prefix(searchText.count) == searchText})
collectionView.reloadData()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.