lal*_*ala 3 uitableview ios swift alamofire
我从Alamofire获取数据viewDidLoad,然后将其放入answerArray.然而,Alamofire连接之前,将numberOfRowsInSection被调用和返回0.我怎样才能通过Alamofire获取数据,然后再拿到eventArray.count的numberOfRowsInSection?
var answerArray = NSMutableArray()
override func viewDidLoad() {
let parameters = [
"id":questionNum
]
Alamofire.request(.POST, "http://localhost:3000/api/v1/questions/question_detail",parameters: parameters, encoding: .JSON)
.responseJSON { (request, response, JSON, error) in
println(JSON!)
// Make models from JSON data
self.answerArray = (JSON!["answers"] as? NSMutableArray)!
}
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return answerArray.count
}
Run Code Online (Sandbox Code Playgroud)
行为很正常.UITableView委托numberOfRowsInSection根据需要调用.
您只需触发self.tableView.reloadData()刷新表即可.
Alamofire.request(.POST, "http://localhost:3000/api/v1/questions/question_detail",parameters: parameters, encoding: .JSON)
.responseJSON { (request, response, JSON, error) in
println(JSON!)
// Make models from JSON data
self.answerArray = (JSON!["answers"] as? NSMutableArray)!
self.tableView.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
我认为更优雅的方法是每次分配时重新加载tableView,answerArray以便每当您从API获得响应时tableView都会自动更新.
将self.tableView.reloadData()进入didSet回调.
var answerArray = NSMutableArray() {
didSet {
self.tableView.reloadData()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
499 次 |
| 最近记录: |