2016-08-13 39 views
0

我是新來的swift。我正在嘗試使用表視圖控制器來創建應用程序。 這是我的代碼。輸出不顯示。打印在外面很好。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:使用Swift'Array'而不是'NSMutableArray'。它使事情變得更容易。並在**類中聲明數據源數組'arr' **。最後但並非最不重要的一點是:我懷疑對'NSMutableArray'的轉換完全可行。 – vadian

+0

TableView控制器與tableView.Just工作相同檢查數組是否爲零? –

回答

0

您需要在Almofire閉包中調用您的reloadTable。如下所示:

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