Reputation: 1521
I am trying to implement a search functionality. which will take input from user and will return result when pressed Search button. I have search This example.
But this example is not useful for me. I have a cade like that.
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
}
Another Function is
extension SearchViewController : UISearchResultsUpdating, UISearchBarDelegate {
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.viewList.removeAll()
getListOfItem(searchBar.text!)
}
}
And my
getListOfItem
taking data from data base and after receiving data i am reloading myUITableView
from that function.
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();
}
This function is getting data from data base and also reloading
myTableView
. Result is not showing until i clicked oncancel
button ofUISearchBar
. Please help me for that thanks.UPDATED I have just resolve that issue after adding some code in
searchBarSearchButtonClicked()
i have addself.resultSearchController?.view.isHidden = true
this line but here i can not scroll my table directly. Means i can scroll my table after on tap on that
Upvotes: 1
Views: 202
Reputation: 1521
After Adding some line of code in my
searchBarSearchButtonClicked
. My problem is getting resolved.
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()
}
Before using
Make Sure
the hierarchy of all line which is written in that function.
Upvotes: 3
Reputation: 336
func updateSearchResults(for searchController: UISearchController) {
self.view.isHidden = false
getListOfItem(searchBar.text!)
}
I think you missed adding this
getListOfItem(searchBar.text!)
infunc updateSearchResults(for searchController: UISearchController)
Upvotes: 0