与Alamofire一起使用时未调用cellForRowAtIndexPath

编程入门 行业动态 更新时间:2024-10-27 10:29:17
本文介绍了与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

本文发布于:2023-08-04 01:27:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Alamofire   cellForRowAtIndexPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!