当我在CategoryVc中选择一个单元格时,它应该弹出MainVc并在CollectionView中显示新数据,但事实并非如此。它仅在我运行应用程序时加载数据。
我检查了一下,选择了单元格后,selectedCategory var获得了正确的String并调用了刷新函数。我不明白怎么了。
编辑:如果我推视图控制器而不是将其弹出,它将起作用。但是我需要弹出它。
它还会打印正确的选择
mainVc是一个UICollectionViewCell
主电源
static var chosenCategory = "General"
func fetchQuotes() {
CATEGORIES_REF.child(QuoteCell.chosenCategory).observe(.childAdded) { (snapshot) in
let quoteId = snapshot.key
CATEGORIES_REF.child(QuoteCell.chosenCategory).child(quoteId).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
let quote = Quote(quoteId: quoteId, dictionary: dictionary)
self.quotes.append(quote)
self.collectionView.reloadData()
print("second: \(QuoteCell.chosenCategory)")
})
}
}
func refresh() {
quotes.removeAll()
fetchQuotes()
print(QuoteCell.chosenCategory)
}
类别Vc
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.navigationController?.popViewController(animated: true)
QuoteCell.chosenCategory = "\(categories[indexPath.item])"
quoteCell.refresh()
}
最佳答案
问题是您在刷新数据之前弹出视图控制器。尝试像这样更改顺序:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
QuoteCell.chosenCategory = "\(categories[indexPath.item])"
quoteCell.refresh()
self.navigationController?.popViewController(animated: true)
}