swift 2 UITableViewController

时间:2016-08-13 07:46:39

标签: swift tableview

我是swift的新手。我正在尝试使用表视图控制器创建一个应用程序。 这是我的代码。输出不显示。打印在外面工作得很好。

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

1 个答案:

答案 0 :(得分:0)

您需要在Almofire闭包内调用reloadTable。如下:

PATCH