检索数据后不重新加载集合视图
Collection view not reloading after retrieving data
情况:我正在从 Firebase 中提取数据。拉完数据后,我想update/reload我的collectionViewtable.
问题:collectionView 不更新。这是带有一些解释的代码。
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
mostPopularCollectionView.dataSource = self
mostPopularCollectionView.delegate = self
getAllProducts { (returnedProductArray) in
self.allProducts = returnedProductArray
self.mostPopularCollectionView.reloadData()
}
}
- 函数 getAllProducts 工作正常。如果我在闭包中打印 allProducts.count,我会得到正确的数字 (3).
- 如果我在闭包外打印 allProducts.count,我的计数为零。
我尝试将 getAllProducts 函数放在 viewWillAppear 中,但没有解决问题
扩展 FeedTableViewController:UICollectionViewDataSource,UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mostPopularCell", for: indexPath) as? MostPopularCollectionViewCell else {return UICollectionViewCell()}
if allProducts.count > 0 {
let product : Product = allProducts[indexPath.row]
if let productImageUrl = product.imageUrlArray.first {
cell.upadateCellUI(forProductName: product.title, forProductImage: productImageUrl, forProductPrice: product.price)
}
return cell
} else {
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let productVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "productVC") as! ProductViewController
productVC.product = allProducts[indexPath.row]
present(productVC, animated: true, completion: nil)
}
}
好消息是,当我点击任何项目时,在下一个 viewController 出现时选择了正确的产品。
所以唯一的问题是如何让 collectionView 在从 Firebase 检索数据后重新加载?非常感谢任何帮助
这是用于从 Firebase 检索所有数据的 getAllProducts 函数。
//MARK:- Retrieve all products from Firebase
func getAllProducts (handler: @escaping (_ allProducts: [Product]) -> ()) {
//TODO:- Create an empty array to store all product fetched from Firebase
var productArray = [Product]()
var imageUrlArray = [String]()
//TODO:- Create reference to Firebase database
let DB = Database.database().reference()
//TODO:- Create reference to products
let REF_PRODUCTS = DB.child("Product")
//TODO:- Snapshot of all products in database
REF_PRODUCTS.observeSingleEvent(of: .value) { (allProductsSnapshot) in
guard let allProductsSnapshot = allProductsSnapshot.children.allObjects as? [DataSnapshot] else {return}
for product in allProductsSnapshot {
let title = product.childSnapshot(forPath: "name").value as! String
let price = product.childSnapshot(forPath: "price").value as! String
let id = product.childSnapshot(forPath: "id").value as! Int
let viewCount = product.childSnapshot(forPath: "viewCount").value as! Int
let description = product.childSnapshot(forPath: "description").value as! String
let REF_IMAGEURL = REF_PRODUCTS.child(String(id)).child("image")
REF_IMAGEURL.observeSingleEvent(of: .value, with: { (allImageUrlSnapshot) in
guard let allImageUrlSnapshot = allImageUrlSnapshot.children.allObjects as? [DataSnapshot] else {return}
for imageUrl in allImageUrlSnapshot {
let imageUrl = imageUrl.value as! String
imageUrlArray.append(imageUrl)
}
})
let product = Product(title: title, price: price, imageUrlArray: imageUrlArray, description: description, viewCount: viewCount, id: id)
productArray.append(product)
}
handler(productArray)
}
}
您的 viewDidLoad 如下所示:
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
mostPopularCollectionView.dataSource = self
mostPopularCollectionView.delegate = self
getAllProducts { (returnedProductArray) in
self.allProducts = returnedProductArray
}
self.mostPopularCollectionView.reloadData()
}
这可能是由于自动布局问题,我卡在同一个案例中并解决了单元格的自动布局问题,启用调试日志以在 xcode 上查看。看看是否有任何自动布局问题,记住内容的大小应该小于集合视图的内容
您应该始终在主线程上更新您的UI元素。这里也不例外。只需在主线程上执行重新加载代码。
dispatch_async(dispatch_get_main_queue(), {
self.mostPopularCollectionView.reloadData()
})
对于Swift 3:
DispatchQueue.main.async {
self.mostPopularCollectionView.reloadData()
}
情况:我正在从 Firebase 中提取数据。拉完数据后,我想update/reload我的collectionViewtable.
问题:collectionView 不更新。这是带有一些解释的代码。
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
mostPopularCollectionView.dataSource = self
mostPopularCollectionView.delegate = self
getAllProducts { (returnedProductArray) in
self.allProducts = returnedProductArray
self.mostPopularCollectionView.reloadData()
}
}
- 函数 getAllProducts 工作正常。如果我在闭包中打印 allProducts.count,我会得到正确的数字 (3).
- 如果我在闭包外打印 allProducts.count,我的计数为零。
我尝试将 getAllProducts 函数放在 viewWillAppear 中,但没有解决问题
扩展 FeedTableViewController:UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mostPopularCell", for: indexPath) as? MostPopularCollectionViewCell else {return UICollectionViewCell()} if allProducts.count > 0 { let product : Product = allProducts[indexPath.row] if let productImageUrl = product.imageUrlArray.first { cell.upadateCellUI(forProductName: product.title, forProductImage: productImageUrl, forProductPrice: product.price) } return cell } else { return cell } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let productVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "productVC") as! ProductViewController productVC.product = allProducts[indexPath.row] present(productVC, animated: true, completion: nil) }
}
好消息是,当我点击任何项目时,在下一个 viewController 出现时选择了正确的产品。
所以唯一的问题是如何让 collectionView 在从 Firebase 检索数据后重新加载?非常感谢任何帮助
这是用于从 Firebase 检索所有数据的 getAllProducts 函数。
//MARK:- Retrieve all products from Firebase
func getAllProducts (handler: @escaping (_ allProducts: [Product]) -> ()) {
//TODO:- Create an empty array to store all product fetched from Firebase
var productArray = [Product]()
var imageUrlArray = [String]()
//TODO:- Create reference to Firebase database
let DB = Database.database().reference()
//TODO:- Create reference to products
let REF_PRODUCTS = DB.child("Product")
//TODO:- Snapshot of all products in database
REF_PRODUCTS.observeSingleEvent(of: .value) { (allProductsSnapshot) in
guard let allProductsSnapshot = allProductsSnapshot.children.allObjects as? [DataSnapshot] else {return}
for product in allProductsSnapshot {
let title = product.childSnapshot(forPath: "name").value as! String
let price = product.childSnapshot(forPath: "price").value as! String
let id = product.childSnapshot(forPath: "id").value as! Int
let viewCount = product.childSnapshot(forPath: "viewCount").value as! Int
let description = product.childSnapshot(forPath: "description").value as! String
let REF_IMAGEURL = REF_PRODUCTS.child(String(id)).child("image")
REF_IMAGEURL.observeSingleEvent(of: .value, with: { (allImageUrlSnapshot) in
guard let allImageUrlSnapshot = allImageUrlSnapshot.children.allObjects as? [DataSnapshot] else {return}
for imageUrl in allImageUrlSnapshot {
let imageUrl = imageUrl.value as! String
imageUrlArray.append(imageUrl)
}
})
let product = Product(title: title, price: price, imageUrlArray: imageUrlArray, description: description, viewCount: viewCount, id: id)
productArray.append(product)
}
handler(productArray)
}
}
您的 viewDidLoad 如下所示:
var allProducts = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
mostPopularCollectionView.dataSource = self
mostPopularCollectionView.delegate = self
getAllProducts { (returnedProductArray) in
self.allProducts = returnedProductArray
}
self.mostPopularCollectionView.reloadData()
}
这可能是由于自动布局问题,我卡在同一个案例中并解决了单元格的自动布局问题,启用调试日志以在 xcode 上查看。看看是否有任何自动布局问题,记住内容的大小应该小于集合视图的内容
您应该始终在主线程上更新您的UI元素。这里也不例外。只需在主线程上执行重新加载代码。
dispatch_async(dispatch_get_main_queue(), {
self.mostPopularCollectionView.reloadData()
})
对于Swift 3:
DispatchQueue.main.async {
self.mostPopularCollectionView.reloadData()
}