cellForRowAtIndexPath not called when using with Alamofire [duplicate]

蓝咒 提交于 2019-12-11 08:08:07

问题


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.

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

回答1:


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



回答2:


if let data = response.result.value{
                print(response.result.value)

                self.userArray = data as! [AnyHashable]
                print(self.groupArray)
                print(self.groupArray.count)

                self.tableview.reloadData()

 }
 break


来源:https://stackoverflow.com/questions/51871952/cellforrowatindexpath-not-called-when-using-with-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!