在我的應用中,授權后,用戶被帶到一個螢屏,根據指定的引數顯示新聞,新聞通過 API 傳輸。
在viewWillAppear方法中,觸發getUserSettings方法,其中觸發fetchNewsData方法,用news填充陣列,基于這個陣列,形成集合單元。該陣列填充了來自資料庫的實際資料,其中包含用戶設定。我的代碼如下:
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 值進行更新
What needs to be changed to make this work correctly?
uj5u.com熱心網友回復:
根據您發布的代碼,我猜您需要先清除 newsArray,然后再將內容加載到其中。在撰寫代碼時,您可以向其附加新訊息。這將導致您不斷添加而不是替換那里的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/366174.html
