2016-08-13 8 views
0

私は迅速です。私は、テーブルビューコントローラを使用してアプリケーションを作成しようとしています。 ここに私のコードです。出力は表示されません。印刷は外でうまく動作します。swift 2 UITableViewController

import UIKit 
import Alamofire 
import SwiftyJSON 
let url :String = "https://testurl.com" 
var arr:NSMutableArray = NSMutableArray() 


class AlertTableViewController: UITableViewController{ 

    override func viewDidLoad() { 

     super.viewDidLoad() 
     Alamofire.request(.GET, url).validate().responseJSON { response in 
      switch response.result { 
      case .Success: 
       if let value = response.result.value { 
        let json = JSON(value) 
        arr = (response.result.value) as! NSMutableArray 

        print("JSON: \(json)") 
        print (arr.count) 
       } 
      case .Failure(let error): 
       print(error) 
      } 
     } 

     dispatch_async(dispatch_get_main_queue()){ 
      self.tableView.reloadData() 
     } 

     // 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() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return arr.count 

    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("AlertTableViewCell", forIndexPath: indexPath) as! AlertTableViewCell 

     // Configure the cell... 
     //cell.Label1.text = json["ALERTID"].stringValue 
     // cell.Labl1.text=json[0]["ALERTID"].stringValue 

     let dict = arr[indexPath.row] as! NSDictionary 
     print("dict") 

     print(dict["ALERTID"]) 
     //cell.Label1.text = String (dict["ALERTID"]!) 

     cell.Label1.text = "Hello" 
     cell.Label2.text = String (dict["PERSID"]!) 
     return cell 
    } 
} 
+0

JSONレスポンスを表示します。 –

+0

クラシック:Alamofireリクエストは非同期で動作します。完了ハンドラの**テーブルビュー**をリロードするコードを入れてください。 PS: 'NSMutableArray'ではなくSwift' Array'を使います。それは事をもっと簡単にします。クラス内にデータソース配列 'arr' **を宣言します。最後は重要なことですが、NSMutableArrayへのキャストはまったく機能しません。 – vadian

+0

TableView ControllerはtableView.Justと同じ働きをします。チェック配列はnilですか? –

答えて

0

reloadTableをAlmofireクロージャ内で呼び出す必要があります。次のようなもの:

override func viewDidLoad() { 

    super.viewDidLoad() 
    Alamofire.request(.GET, url).validate().responseJSON { response in 
     switch response.result { 
     case .Success: 
      if let value = response.result.value { 
       let json = JSON(value) 
       arr = (response.result.value) as! NSMutableArray 

       print("JSON: \(json)") 
       print (arr.count) 
       //This is what i changed 
       dispatch_async(dispatch_get_main_queue()){ 
        self.tableView.reloadData() 
       } 
      } 
     case .Failure(let error): 
      print(error) 
     } 
    } 
} 
関連する問題