我想使用MVC模式,我用的Alamofire API获得响应的JSON数据。我无法发送数据来更新我的模型中的tableview。帮助我使用json值更新我的表格。如何将模型的(json)值发送到视图控制器?
这是我的API:
class func showData(completionHandler:(model)->()){
var arrRes = [[String:AnyObject]]()
Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (req, res, json) -> Void in
let swiftyJsonVar = JSON(json.value!)
if let resData = swiftyJsonVar["contacts"].arrayObject {
arrRes = resData as! [[String:AnyObject]]
print(arrRes)
if arrRes.count > 0 {
let name = swiftyJsonVar["contacts"]["name"].stringValue
let email = swiftyJsonVar["contacts"]["email"].stringValue
let detail = model(name: name, email: email)
completionHandler(detail)
}
}
}
}
这是我的控制器:
@IBOutlet var appsTableView: UITableView!
var tableData:[model] = [model]()
override func viewDidLoad() {
super.viewDidLoad()
self.appsTableView.reloadData()
self.appsTableView.backgroundColor = UIColor.orangeColor()
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = appsTableView.dequeueReusableCellWithIdentifier("MyTestCell") as! newTabelcell
let dict = self.tableData[indexPath.row]
cell.name.text = dict.name
cell.mailid.text = dict.email
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func configureTableView() {
appsTableView.rowHeight = UITableViewAutomaticDimension
appsTableView.estimatedRowHeight = 100.0
self.appsTableView.reloadData()
}
这是我的模型:
class model: NSObject {
var name:String!
var email:String!
init(name:String , email:String){
super.init()
self.name = name
self.email = email
}
}
只有tableview中和两个标签是我有插入单元格中;一个标签用于显示json名称,另一个用于显示json电子邮件。