Skip to content

Instantly share code, notes, and snippets.

@openopen114
Created June 30, 2016 03:06
Show Gist options
  • Select an option

  • Save openopen114/2e8731567bb78b5603cd9017e08b7ce1 to your computer and use it in GitHub Desktop.

Select an option

Save openopen114/2e8731567bb78b5603cd9017e08b7ce1 to your computer and use it in GitHub Desktop.
import UIKit
import Alamofire
class RentViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,NSURLSessionDataDelegate,NSURLSessionDelegate {
// outlet
@IBOutlet weak var toyTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// getJsonData(jsonURl) // download json data by url
getJsonData(dataURL.rentJsonURL,jsonImageURL:dataURL.rentJsonImageURL) // download json data by url
//添加刷新
refreshControl.addTarget(self, action: #selector(RentViewController.refreshData), forControlEvents: UIControlEvents.ValueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "刷新資料")
toyTableView.addSubview(refreshControl)
}
// 刷新数据
func refreshData() {
getJsonData(dataURL.rentJsonURL,jsonImageURL:dataURL.rentJsonImageURL) // download json data by url
}
//MARK: - GET JSON
func getJsonData(jsonURL: String,jsonImageURL: String){
// remove/clear cache
NSURLCache.sharedURLCache().removeAllCachedResponses()
/*********** Alamofire.request 001 ***********/
Alamofire.request(.GET,jsonImageURL).responseJSON { (response) in
print("in Alamofire.request 001")
if let JSONImg = response.result.value{
//check response.result success or not
switch response.result {
case .Success:
//把得到的JSON数据转为数组
print("========= 001 response case .Success:")
case .Failure(let error):
print("========= 001 response case .Failure:")
print(error)
}
// Main Queue
dispatch_async(dispatch_get_main_queue(),{
self.toyTableView.reloadData() // reload data
self.refreshControl.endRefreshing()
});
print("Alamofire 001: End")
}
}// End of Alamofire 001
/*********** Alamofire.request 002 ***********/
Alamofire.request(.GET,jsonURL).responseJSON { (response) in
print("in Alamofire.request 002")
if let JSONImg = response.result.value{
//check response.result success or not
switch response.result {
case .Success:
//把得到的JSON数据转为数组
print("========= 002 response case .Success:")
case .Failure(let error):
print("========= 002 response case .Failure:")
print(error)
}
// Main Queue
dispatch_async(dispatch_get_main_queue(),{
self.toyTableView.reloadData() // reload data
self.refreshControl.endRefreshing()
});
print("Alamofire 002: End")
}
}// End of Alamofire 002
}// End of getJsonData function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment