这种方法有效,但问题是我相信在alamofire有机会获取json文件之前会生成该表。如果我在加载后打印出self.dates它显示正常。有没有办法在加载时暂停视图。或者我在这里采取了一种完全错误的做法?感谢
import UIKit
import Alamofire
class TableViewController: UITableViewController {
var dates: [String] = []
var times: [String] = []
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
loadPosts()
}
func loadPosts() {
Alamofire.request(.GET, "http://localhost:5000/listposts")
.responseSwiftyJSON {(request, response, jsonObj, error) in
for index in 0...jsonObj.count-1 {
self.dates.append(jsonObj[index]["appDate"].stringValue)
self.times.append(jsonObj[index]["appTime"].stringValue)
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView!.reloadData()
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPress() {
self.tableView!.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dates.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = self.dates[indexPath.row]
cell.detailTextLabel?.text = self.times[indexPath.row]
return cell
}
}