与Alamofire一起使用时未调用cellForRowAtIndexPath [英] cellForRowAtIndexPath not called when using with Alamofire

查看:110
本文介绍了与Alamofire一起使用时未调用cellForRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个函数,该函数调用用户api
的列表,并将其放在 viewDidLoad 中,以便可以在中获得列表。加载屏幕后,TableViewCell 就会出现。

I made a function that calls list of user api and I put it in viewDidLoad so that I can get the list in TableViewCell as soon as screen get loaded.

但是问题出在调用 numberOfRowsInSection ,Alalmofire.rquest被自动调用,而 cellForRowAtIndexPath 没有被调用,因此 TableView 为空。

But the problem is after calling of numberOfRowsInSection, Alalmofire.rquest gets called automatically and cellForRowAtIndexPath not getting called hence TableView is empty.

 func userList(){

    let header = [

         "some header":"SomeHeaderValue"
    ]

    var request = BaseUrl + "userList"

    let parameters = ["":""]

    var encodingFormat: ParameterEncoding = URLEncoding()
    if request == "" {
        encodingFormat = JSONEncoding()
    }

    Alamofire.request(request, method: .get, parameters: parameters, encoding: encodingFormat, headers: header).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)

                self.userArray = data as! [AnyHashable]

            }
            break

        case .failure(_):
            print(response.result.error)
            break
        }
   }
}


推荐答案

网络调用是异步的,需要时间。收到数据后,您需要重新加载 tableView

Network calls are asynchronous and take time. You need to reload the tableView after receiving the data.

 Alamofire.request(request, method: .get, parameters: parameters, encoding: encodingFormat, headers: header).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if let data = response.result.value{
                print(response.result.value)

                self.userArray = data as! [AnyHashable]
                print(self.groupArray)
                print(self.groupArray.count)
                DispatchQueue.main.async {
                    tableView.reloadData()
                }
            }

            break

        case .failure(_):
            print(response.result.error)
            break
        }
   }

这篇关于与Alamofire一起使用时未调用cellForRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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