我需要创建一个搜索栏来搜索产品。为此,我使用searchcontroller,我有一个api用于按名称搜索产品。我想要这个:从api搜索时的动画指示处理。我怎么解决这个问题? 这是我从api搜索的代码:
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!))
}
}
}
}
这是我的搜索结果更新:
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()
}
答案 0 :(得分:0)
您需要像这样更改代码 像这样改变你的getTover
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
indicator.stopAnimationg()
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!))
}
indicator.stopAnimationg()
for item in data {
if item.productTitle.containsString(searchController.searchBar.text!){
filteredTableData.append(item)
}
}
self.tableView.reloadData()
}
}
}
并像这样更改你的updateSearchResultsForSearchController
func updateSearchResultsForSearchController(searchController: UISearchController){
searchController.searchBar.addSubview(indicator)
indicator.startAnimating()
getTovar()
}
希望这会有所帮助。