我正在尝试实现搜索功能。它将从用户获取输入,并在按下搜索按钮时返回结果。 我有搜索This example。
但这个例子对我没用。我有这样的嘲笑。
override func viewDidLoad() {
super.viewDidLoad()
// Setup the Location Search bar Controller
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController?.searchResultsUpdater = locationSearchTable
searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.delegate = self
navigationItem.titleView = resultSearchController?.searchBar
resultSearchController?.hidesNavigationBarDuringPresentation = false
resultSearchController?.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
}
另一个功能是
extension SearchViewController : UISearchResultsUpdating, UISearchBarDelegate {
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.viewList.removeAll()
getListOfItem(searchBar.text!)
}
}
我的
getListOfItem
从数据库中获取数据,在收到数据后,我正在从该函数重新加载UITableView
。
func getListOfItem(_ query : String){
if !loadingData {
self.viewList.removeAll()
}
let request = NSMutableURLRequest(url: URL(string: MyUrl.API_SERVER_URL)!)
request.httpMethod = "POST"
let postString = "tag=search&p=\(query)&page=\(String(pageNo))"
print("postString=\(postString)")
DispatchQueue.main.async {
self.startSpinner()
}
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if(error != nil){
self.loadingData = false
DispatchQueue.main.async {
self.stopSpinner()
}
let nsError = error! as NSError
let dialog = UIAlertController(title: "Internal Server Error?", message: nsError.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
dialog.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
DispatchQueue.main.async(execute: {
self.present(dialog, animated: true, completion: nil)
})
} else{
self.loadingData = false
do {
var success : Int = 0
let jsonObj = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String : AnyObject]
success = jsonObj["success"]! as! Int
if success == 1 {
let json = jsonObj["data"]as? NSArray
self.pageNo = self.pageNo + 1
DispatchQueue.main.async {
for i in 0 ..< json!.count {
let productObj = json![i]as? [String: AnyObject]
let newModel = Search()
newModel.attributeId = productObj!["AttributeID"] as? String
newModel.attributePrice = productObj!["AttributePrice"] as? String
newModel.imageURL = productObj!["ImageURL"] as? String
newModel.inventoryCurrent = productObj!["InventoryCurrent"] as? String
newModel.pageText = productObj!["PageText"] as? String
newModel.productDescription = productObj!["ProductDescription"] as? String
newModel.pageURL = productObj!["PageURL"] as? String
newModel.productID = productObj!["ProductID"] as? String
newModel.productName = productObj!["ProductName"] as? String
newModel.thumbURL = productObj!["ThumbURL"] as? String
newModel.internationalShipping = productObj!["internationalShipping"] as? String
newModel.maxQty = productObj!["maxQty"] as? String
newModel.productDisplay = productObj!["productDisplay"] as? String
self.viewList.append(newModel)
}
}
}
DispatchQueue.main.async {
print("reload")
self.checkItems()
self.viewListTable.reloadData()
//self.view.layoutIfNeeded()
}
} catch {
}
DispatchQueue.main.async(execute: {
self.stopSpinner()
})
}
}
task.resume();
}
此功能从数据库获取数据并重新加载
myTableView
。在我点击cancel
的UISearchBar
按钮之前,结果才会显示。 请帮助我谢谢。已更新 我在
后滚动我的桌子searchBarSearchButtonClicked()
中添加了一些代码之后才解决了这个问题我添加了self.resultSearchController?.view.isHidden = true
这一行但是在这里我不能直接滚动我的表。意味着我可以点击那个
答案 0 :(得分:3)
在
searchBarSearchButtonClicked
中添加一些代码后。我的问题正在得到解决。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.viewList.removeAll()
self.searchBar.resignFirstResponder()//This Line Remove the keyboard
self.resultSearchController?.view.isHidden = true // This line Hide current View
self.viewListTable.reloadData()
pageNo = 1
self.searchStr = searchBar.text! // This Line taking a search string into global variable for future use.
self.resultSearchController?.isActive = false // This Line for inActivate searchView.
getListOfItem()
}
希望这可以帮助那些遇到同样问题的人,然后再使用
Make Sure
在该函数中编写的所有行的层次结构。
感谢那些试图帮助我的人。
答案 1 :(得分:0)
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
getListOfItem(searchBar.text!)
}
我认为您错过了在
中添加此getListOfItem(searchBar.text!)
func updateSearchResults(for searchController: UISearchController)