为什么collectionView中我的单元格的形成速度比接收数据的方法快



在我的应用程序中,授权后,用户被带到一个根据指定参数显示新闻的屏幕,新闻通过API传输。

在viewWillAppear方法中,触发getUserSettings方法,其中触发fetchNewsData方法,该方法用新闻填充数组,基于该数组形成集合单元格。数组中填充了数据库中的实际数据,其中包含用户设置。我的代码如下:

class NewsViewController: UIViewController {

let networkManager = NetworkManager()

var newsArray = [NewsModel]()
var totalResult:Int = 0
var ref: DatabaseReference!

@IBOutlet weak var collectionView: UICollectionView!

@IBAction func settingsAction(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "settingsSegue", sender: nil)
}

@IBAction func unwindSegueToNewsScreen(segue: UIStoryboardSegue) {

guard segue.identifier == "unwindFromSettingsSegue" else { return }
}

private func getUserSettings(completion: @escaping (UserModel) -> ()) {
guard let currentUser = Auth.auth().currentUser else { return }

var user: UserModel!
user = UserModel(user: currentUser)
ref = Database.database().reference(withPath: "users").child(String(user.uid))
ref.observe(.value) { snapshot in
guard let snapshotValue = snapshot.value as? [String : String] else { return }
user.userCountry = snapshotValue["country"]
user.userCategory = snapshotValue["category"]
completion(user)
}
}

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

getUserSettings { [weak self] user in
self?.networkManager.fetchNewsData(forCoutry: user.userCountry ?? "us", category: user.userCategory ?? "sports") { [weak self] newsDataModel in

self?.totalResult = newsDataModel.totalResults

for article in newsDataModel.articles {
let news = NewsModel(newsTitle: article.title,
urlToNewsWebSite: article.url,
authorWebSiteName: article.source.name,
urlToImage: article.urlToImage ?? "" )

self?.newsArray.append(news)
}
DispatchQueue.main.async {
self?.collectionView.reloadData()
}

}
}
}

}

extension NewsViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! NewsCell
cell.configureCell()
cell.initData(news: newsArray[indexPath.item])
return cell
}
}

我想让用户能够在额外的屏幕上更改设置

在第二个屏幕上,用户将更新他们在数据库中的数据,我希望当我返回到新闻屏幕时,表再次加载更新的数据,但我遇到了问题。

当表第一次加载数据时,触发单元形成方法:

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! NewsCell
cell.configureCell()
cell.initData(news: newsArray[indexPath.item])
return cell
}

但由于最初包含消息的数组是空的,因此无法形成单元格。newsArray.count // = 0

但由于最初带有新闻的数组是空的,因此无法形成单元格,因此启动了填充数组并重新加载单元格的方法,现在形成了单元格。但当我从设置屏幕进入新闻屏幕时,单元格已经有数据了,因为newsArray不是空的,屏幕上显示的是不相关信息的单元格。

我尝试使用collectionView.reloadData()更新集合,但没有帮助,我也尝试了这个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "unwindFromSettingsSegue" else { return }
let dvc = segue.destination as! NewsViewController
dvc.collectionView.reloadData()
}

这不起作用,因为集合只是用在进入设置屏幕之前生成的newsArray值更新

需要更改哪些内容才能使其正常工作?

根据您发布的代码,我猜您需要在再次将内容加载到newsArray之前清除它。当你现在编写代码时,你会在其中添加新的消息。这会导致你不断地添加到其中,而不是替换原来的内容。

最新更新