Ios UI搜索栏未显示结果,直到未单击“取消”按钮
我正在尝试实现搜索功能。它将接收用户的输入,并在按下搜索按钮时返回结果。 我有搜索 但是这个例子对我没有用处。我有一个这样的凯德Ios UI搜索栏未显示结果,直到未单击“取消”按钮,ios,iphone,swift,xcode,uisearchbar,Ios,Iphone,Swift,Xcode,Uisearchbar,我正在尝试实现搜索功能。它将接收用户的输入,并在按下搜索按钮时返回结果。 我有搜索 但是这个例子对我没有用处。我有一个这样的凯德 override func viewDidLoad() { super.viewDidLoad() // Setup the Location Search bar Controller let locationSearchTable = storyboard!.instantiateViewController(withIdentifier:
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
}
另一个功能是
我的getListOfItem
从数据库中获取数据,收到数据后,我将从该函数重新加载我的UITableView
func getListOfItem(query:String){
如果!加载数据{
self.viewList.removeAll()
}
let request=NSMutableURLRequest(url:url(字符串:MyUrl.API\u SERVER\u url)!)
request.httpMethod=“POST”
让postString=“tag=search&p=\(查询)&page=\(字符串(页码))”
打印(“postString=\(postString)”)
DispatchQueue.main.async{
self.startSpinner()
}
request.httpBody=postString.data(使用:String.Encoding.utf8)
让task=URLSession.shared.dataTask(其中:request作为URLRequest){
数据、响应、错误
如果(错误!=nil){
self.loadingData=false
DispatchQueue.main.async{
self.stopSpinner()
}
设nsError=error!作为nsError
let dialog=UIAlertController(标题:“内部服务器错误?”,消息:nsError.localizedDescription,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“确定”,样式:UIAlertActionStyle.default,处理程序:nil))
DispatchQueue.main.async(执行:{
self.present(对话框,动画:true,完成:nil)
})
}否则{
self.loadingData=false
做{
var成功:Int=0
让jsonObj=try JSONSerialization.jsonObject(使用:data!,选项:.allowFragments)作为![String:AnyObject]
success=jsonObj[“success”]!as!Int
如果成功==1{
将json=jsonObj[“数据”]设为?NSArray
self.pageNo=self.pageNo+1
DispatchQueue.main.async{
对于0中的i..
此函数用于从数据库获取数据,并重新加载myTableView
。在我单击UISearchBar
的取消按钮之前,结果不会显示。
请帮帮我,谢谢
已更新
在searchBarSearchButtonClicked()
中添加一些代码后,我刚刚解决了这个问题。我已经添加了self.resultSearchController?.view.ishiden=true
这一行,但在这里我无法直接滚动我的表。意味着我可以在点击后滚动我的表格
我想您没有在func updateSearchResults(对于searchController:UISearchController)中添加这个getListoItem(searchBar.text!)
在mySearchBarSearchButtonClick中添加一些代码行后
。我的问题正在得到解决
在使用之前,请确保该函数中写入的所有行的层次结构
添加带有字符串参数的getListOfItem方法。@我忘了在我的问题中添加该内容,请现在检查。我已经将其添加到我的函数中。除了重新加载之外,您是否在使用此方法进行任何操作?@KKRocks正在从服务器获取数据,获取数据后,请尝试重新加载我的UITableView
我看不出如何从getListOfItem中获取数据。让我尝试一下,很快会返回给您,还有一件事我希望得到结果搜索
按钮按下。它能工作吗?。不,它没有工作,在单击“取消”按钮后仍然得到结果。您能共享TableViewDataSource方法的代码吗?我刚刚在searchBarSearchButtonClicked()中添加了一些代码后解决了这个问题
我已经添加了self.resultsearch controller?.view.ishiden=true
这一行,但在这里我无法直接滚动表格。意味着我可以在点击OK后滚动我的表格。但请这样做,这会帮助其他人。
extension SearchViewController : UISearchResultsUpdating, UISearchBarDelegate {
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.viewList.removeAll()
getListOfItem(searchBar.text!)
}
}
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();
}
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
getListOfItem(searchBar.text!)
}
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()
}