在我的应用程序中,我有这个类从我的服务器获取数据:
class Api{ func loadOffers(completion:(([Offers])-> Void),offer_id: String,offerStatus:String){ let myUrl = NSURL(string: "http://www.myServer.PHP/api/v1.0/offers.PHP") let request = NSMutableURLRequest(URL: myUrl!) request.HTTPMethod = "POST" let postString = "offer_id=\(offer_id)&offerStatus=\(dealStatus)&action=show" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: true) let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data,response,error in if error != nil { println("error\(error)") }else{ var err:NSError? let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers,error: nil) if let dict = jsonObject as? [String: AnyObject] { if let myOffers = dict["offers"] as? [AnyObject] { var offers = [Offers]() for offer in myOffers{ let offer = Offers(dictionary: offer as! NSDictionary) offers.append(offer) let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT dispatch_async(dispatch_get_global_queue(priority,0 )){ dispatch_async(dispatch_get_main_queue()){ completion(offers) } } } } } } } task.resume() } }
然后在我的View Controller中加载模型:
class MyViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { var offers: [Offers]! func loadModel() { let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view,animated: true) loadingNotification.mode = MBProgressHUDMode.Indeterminate loadingNotification.labelText = "updating your offers..." offers = [Offers]() let api = Api() api.loadOffers(didLoadOffers,offer_id: dealIdent!,offerStatus: "open") } func didLoadOffers(offers:[Offers]){ self.offers = offers self.tableView.reloadData() MBProgressHUD.hideAllHUDsForView(self.view,animated: true) self.refreshControl.endRefreshing() } override func viewWillAppear(animated: Bool) { loadModel() } }
除了当JSON字典为空时,一切都有效,这意味着没有提供MBProgressHUD保持旋转.
我想停止活动指示器添加一个子视图,而不是说没有优惠.任何建议将不胜感激.
我试过了:
if offers.isEmpty {
MBProgressHUD.hideAllHUDsForView(self.view,animated:true)
}
并且
if offers == 0 {
MBProgressHUD.hideAllHUDsForView(self.view,animated:true)
}
但它不起作用
谢谢