![](/img/trans.png)
[英]Why sometime heightForRowAtIndexPath is called before numberOfRowsInSection?
[英]numberOfRowsInSection is called before Alamofire connection
我在viewDidLoad
获取Alamofire的数据,然后将其放入answerArray。 但是,在Alamofire连接之前,调用numberOfRowsInSection
并返回0.如何首先通过Alamofire获取数据,然后在numberOfRowsInSection
获取eventArray.count
?
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
}
行为很正常。 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()
}
我认为每次分配给answerArray
,更优雅的方法是重新加载tableView,以便每当您从API获得响应时tableView都会自动更新。
将self.tableView.reloadData()
移动到didSet
回调中。
var answerArray = NSMutableArray() {
didSet {
self.tableView.reloadData()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.