gpt4 book ai didi

ios - 如何使用 Alamofire、Swift 4 为 UICollectionView 获取 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 14:55:07 25 4
gpt4 key购买 nike

我有一个 UICollectionView,我试图用 来自服务器 的 JSON 数据来填充它。为此,我使用了 Alamofire 库。在 Collection View 中,我们有两种主要方法:

First - 返回 Collection 中的单元格数

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messagesArray!.count
}

第二 - 返回包含内容的单元格

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chatMessage", for: indexPath) as! ChatMessageCell
let messageObj = messagesArray![indexPath.item]
let messageText = messageObj.userText
cell.messageText.text = messageText
return cell
}

为了填充我的收藏,我在 viewDidLoad() 方法中调用了我的 getChatMessages() 方法:

override func viewDidLoad() {
super.viewDidLoad()

setUpChatLogCollection()
setupChatLogCollectionConstraints()
getChatMessages()

}

所以,我遇到了这个问题 - 似乎只有在收集方法完成后,使用 Alamofire 获取服务器数据才开始工作,所以我的收集没有得到任何数据来显示。

伙计们,你们如何使用 Alamofire 获取收藏的数据?

我试过 viewWillAppear(),但它给了我与 viewDidLoad() 相同的结果。

最佳答案

我假设您的数据获取方法在同一个 View Controller 中。因此,一旦 Alamofire 返回数据,将其加载到您的集合中并调用 Collection View 对象上的 reloadData() 方法。

引用:reloadData() on UICollectionView

关于ios - 如何使用 Alamofire、Swift 4 为 UICollectionView 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465915/

25 4 0
文章推荐: css - 当
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com