Ich muss eine Suchleiste erstellen, um das Produkt zu suchen. Dafür benutze ich SearchController und ich habe eine API für die Suche nach Produkten nach Namen. Ich will das: animierende Anzeige bei der Suche aus API verarbeitet. Wie kann ich das lösen? Dies ist mein Code von api für die Suche:Startanzeige bei Suche in der Suchleiste
func getTovar(){
Alamofire.request(.GET, "myApiurl")
.responseJSON { response in
guard response.result.error == nil else {
// got an error in getting the data, need to handle it
print("error calling GET on /todos/1")
print(response.result.error!)
self.indicator.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
return
}
if let value = response.result.value {
// handle the results as JSON, without a bunch of nested if loops
let product = JSON(value)
for (_,subJson):(String, JSON) in product {
let img:NSData
if let src=subJson["sphoto"].string{
if src.containsString("jpg"){
let range = src.startIndex.advancedBy(2)..<src.endIndex
let substring = src[range]
var urlString = "http://admin.unimax.kz/\(substring)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
if let dataFromURL=NSData(contentsOfURL: NSURL(string: urlString)!){
img=dataFromURL
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
//Do something you want
let id=subJson["id"].int!
let name=subJson["name"].string!
let price=subJson["price"].int!
let description=subJson["description"].rawString()
self.data.append(Product(id:id,title: name, img: UIImage(data: img), price: price,desc:description!))
}
}
}
}
Diese meine Suchergebnisse Aktualisierung ist:
func updateSearchResultsForSearchController(searchController: UISearchController)
{
searchController.searchBar.addSubview(indicator)
indicator.stopAnimating()
getTovar()
for item in data {
if item.productTitle.containsString(searchController.searchBar.text!){
filteredTableData.append(item)
}
}
indicator.stopAnimating()
self.tableView.reloadData()
}
Ihr WebService-Aufruf ist asynchron. Es funktioniert also nicht in der von Ihnen gewünschten Reihenfolge. – Larme
Wie kann ich das tun? –