在Alamofire连接之前调用numberOfRowsInSection [英] numberOfRowsInSection is called before Alamofire connection

查看:88
本文介绍了在Alamofire连接之前调用numberOfRowsInSection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 viewDidLoad 中获取Alamofire的数据,然后将其放入answerArray。但是,在Alamofire连接之前,调用 numberOfRowsInSection 并返回0.如何首先获取Alamofire的数据,然后获取 eventArray.count at numberOfRowsInSection

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

}


推荐答案

行为正常。 UITableView 委托根据需要调用 numberOfRowsInSection

The behavior is normal. UITableView delegate invokes numberOfRowsInSection as needed.

所有你需要触发 self.tableView.reloadData()来刷新表。

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

这篇关于在Alamofire连接之前调用numberOfRowsInSection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆