I get data by Alamofire in viewDidLoad
, then put it into answerArray. However, before Alamofire connection, the numberOfRowsInSection
is invoked and returns 0. How can I get data by Alamofire first, then get eventArray.count
at 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
}
The behavior is normal. UITableView
delegate invokes numberOfRowsInSection
as needed.
All you need is to trigger self.tableView.reloadData()
to refresh the table.
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()
}
I think more elegant way would be reloading tableView every time you assign to answerArray
so that tableView will be updated automatically whenever you get a response from your API.
Move self.tableView.reloadData()
into didSet
callback.
var answerArray = NSMutableArray() {
didSet {
self.tableView.reloadData()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.