gpt4 book ai didi

ios - 在 collectionView 的函数之前快速运行函数

转载 作者:可可西里 更新时间:2023-11-01 00:56:42 24 4
gpt4 key购买 nike

我的swift代码中有这个函数

  func myFunc(){
let jsonData2 = try JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]
self.comments = jsonData2
}

在 collectionView 的 numberOfItemsInSection 函数中我有这个

return comments.count

我需要的是首先运行 myFunc,然后运行所有其他 collectionView 的函数。我尝试将 myFunc() 放在 viewDidLoad( ),但没有成功。

最佳答案

您需要将您的comments 数组定义为空,然后在您的viewWillAppear 中加载您的相关数据

class YourViewController: UIViewController {
var comments : [YourObjects] = []

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.myFunc()
}

func myFunc(){
let jsonData2 = try JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]
self.comments = jsonData2
DispatchQueue.main.async{
self.collectionView.reloadData() //reload data after
}
}

}

希望对你有帮助

关于ios - 在 collectionView 的函数之前快速运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com