3

この種の作品ですが、問題は、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

}
}
4

0 に答える 0