17. import UIKit
importAlamofire
classViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView! //TableView元件宣告
var jsonArray:NSMutableArray? //Json陣列宣告
var newArray: Array<String> = [] //陣列宣告
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self//表格代理指定
tableView.dataSource = self //資料來源指定
Alamofire、Cocoapods、JSON、Open Data
之程式運用
18. //使用 Alamofire 取得網站資料的 Rrquest
Alamofire.request(.GET,
"http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AV&CaseNo2=1&FileType=1&Lang=C&FolderType=") .responseJSON
{ response in
print(response.request) // 原始的 URL 要求
print(response.response) // URL 回應
print(response.data) // 伺服器資料
print(response.result) // 回應的序列化結果
if let JSON = response.result.value { //取得網頁回傳值放入 JSON
self.jsonArray = JSON as? NSMutableArray //存入 JSON 陣列
for item in self.jsonArray! { //從陣列中逐一取值
if let name = item["Name"], myName = name as? String { //取得某欄位值
print(myName) //印出查看
self.newArray.append(myName) //將值放入陣列變數
}
}
self.tableView.reloadData() //表格重載
}
}
}
Alamofire、Cocoapods、JSON、Open Data
之程式運用
19. override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.newArray.count //回傳陣列長度
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.newArray[indexPath.row] 顯示欄位值(從陣列中)
return cell
}
}
Alamofire、Cocoapods、JSON、Open Data
之程式運用
33. import UIKit
class dataTableViewCell: UITableViewCell {
@IBOutlet weak var lblName: UILabel! //Label 元件宣告
@IBOutlet weak var lblTel: UILabel! //Label 元件宣告
@IBOutlet weak var lblAddress: UILabel! //Label 元件宣告
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Alamofire、Cocoapods、JSON、Open Data
之程式運用
34. import UIKit
importAlamofire
class dataTableViewController: UITableViewController {
var jsonArray:NSMutableArray? //Json 陣列宣告
var nameArray: Array<String> = [] //Name 陣列變數宣告
var telArray: Array<String> = [] //Tel 陣列變數宣告
var addressArray: Array<String> = [] //Address 陣列變數宣告
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
Alamofire、Cocoapods、JSON、Open Data
之程式運用