我是Stack Overflow的新手,学习Swift,现在我正在创建一个API调用项目。
我希望在从API获取数据之前使用自定义活动指示器视图。所以请帮我在我的代码中简短问题。
我在项目中导入了JTMaterialSpinner
库。
这是我要显示的微调器(自定义活动指示器视图):
这是我的代码:
var spinerrview : JTMaterialSpinner = JTMaterialSpinner(frame: CGRectZero)
override func viewDidLoad()
{
super.viewDidLoad()
spinerrview.circleLayer.strokeColor = UIColor.greenColor().CGColor spinerrview.circleLayer.lineWidth=2.0
spinerrview.frame.size.height=25.0
spinerrview.frame.size.width=25.0
spinerrview.circleLayer
spinerrview.isAnimating=true
spinerrview.backgroundColor=UIColor.greenColor()
spinerrview.beginRefreshing()
getdata()
spinerrview.endRefreshing()
}
野兔我给出了完整的代码
import UIKit
import Alamofire
import SwiftyJSON
import JTMaterialSpinner
class ViewController: UIViewController ,UITextViewDelegate,UITableViewDataSource{
var arrRes = [[String:AnyObject]]()
var spinerrview : JTMaterialSpinner = JTMaterialSpinner(frame: CGRectZero)
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
spinerrview.circleLayer.strokeColor = UIColor.redColor().CGColor
spinerrview.circleLayer.lineWidth=3.0
spinerrview.circleLayer
spinerrview.isAnimating=true
spinerrview.frame.size.height=25.0
spinerrview.frame.size.width = 25.0
spinerrview.center=table.center
spinerrview.beginRefreshing()
getdata(NSURL(string: "http:api.androidhive.info/contacts/")!, completionHandler: { (success) -> Void in
// When your API call was terminated, control flow goes here.
self.spinerrview.endRefreshing()
if success {
print("success")
} else {
print("fail")
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
typealias CompletionHandler = (success:Bool) -> Void
func getdata(url: NSURL,completionHandler: CompletionHandler) {
Alamofire.request(.GET, url).validate().responseJSON { response in
var flag = false // true if call is succeed,false otherwise
switch response.result {
case .Success(let data):
flag=true
let swiftyJsonVar = JSON(data)
if let resData = swiftyJsonVar["contacts"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
self.table.reloadData()
case .Failure(let error):
flag=false
print("Request failed with error: \(error)")
}
completionHandler(success: flag )
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("jsonCell")!as! TableViewCell
var dict = arrRes[indexPath.row]
cell.name.text = dict["name"] as? String
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrRes.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("gotoview", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "gotoview") {
let indexPat = self.table.indexPathForSelectedRow!
let vc = segue.destinationViewController as! secondViewController
vc.adde = "add "+(arrRes[indexPat.row]["address"] as? String)!
vc.mail = "mail "+(arrRes[indexPat.row]["email"] as? String)!
vc.iid = "id "+(arrRes[indexPat.row]["id"] as? String)!
vc.nam = (arrRes[indexPat.row]["name"] as? String)!
vc.ph = "phone "+(arrRes[indexPat.row]["phone"]!["mobile"]as? String)!
}
}
}
答案 0 :(得分:0)
在你的getData()函数中,添加一个闭包参数,并在其中完成微调器操作。
答案 1 :(得分:0)
您的问题在以下三行:
spinerrview.beginRefreshing()
getdata()
spinerrview.endRefreshing()
spinner在主线程中启动,getdata()启动了,它可能会在后台线程中进行一些工作,所以你不知道它什么时候结束,并且spinner在调用getdata后立即结束。就像在现实生活中一样,当你立即按下开/关按钮时。
为避免此类问题,您可以为getdata()函数创建一个completionHandler(API调用):
typealias CompletionHandler = (success:Bool) -> Void
func getdata(url: NSURL,completionHandler: CompletionHandler) {
// API call code.
let flag = true // true if call is succeed,false otherwise
completionHandler(success: flag)
}
<强>用法强>:
spinerrview.beginRefreshing()
getdata(NSURL(string: "http://...")!, { (success) -> Void in
// When your API call was terminated, control flow goes here.
spinerrview.endRefreshing()
if success {
// API call success
} else {
// API call fail
}
})
<强>更新强>: (在你的新编辑之后添加getdata())
typealias CompletionHandler = (success:Bool) -> Void
func getdata(url: NSURL,completionHandler: CompletionHandler) {
Alamofire.request(.GET, url).validate().responseJSON { response in
var flag = false // true if call is succeed,false otherwise
switch response.result {
case .Success(let data):
flag=true
let swiftyJsonVar = JSON(data)
if let resData = swiftyJsonVar["contacts"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
self.table.reloadData()
case .Failure(let error):
flag=false
print("Request failed with error: \(error)")
}
completionHandler(success: flag )
}
}