ios - UICollectionView 在再次触发 reloadData() 时发现 nil
问题描述
当我从集合视图中删除所有实际数据并添加新数据时,传递错误Unexpectedly found nil ,同时在再次触发reloadData()时解包 Optional 值。这是弹出的代码。
func getAllProducts(){
print("All products before getting again from the func: \(productsAll.count)")
productsAll.removeAll()
refProducts.observeSingleEvent(of: .value) { (snapshot) in
guard let productsDic = snapshot.value as? [String:AnyObject] else {return}
for(_, productSelected) in productsDic{
guard let name = productSelected["name"] as? String else {return}
guard let imageURLString = productSelected["image"] as? String else {return}
guard let price = productSelected["price"] as? String else {return}
guard let description = productSelected["description"] as? String else {return}
guard let imgURL = URL(string: imageURLString) else {return}
guard let data = try? Data(contentsOf: imgURL) else {return}
guard let img = UIImage(data: data) else {return}
let productData = Product(productImage: img, productName: name, productPrice: price, productDescription: description, productRecommended: false)
self.productsAll.append(productData)
print("We have \(self.productsAll.count) products in All products")
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
解决方案
推荐阅读
- php - 如何使用 preg_replace 在 php 中删除括号并将“空白”替换为“-”
- html - Power Automate Desktop - 选择器生成器:根据 HTML 属性选择 UI 元素
- wso2 - 如何使用 OMElement - WSO2 APIM 版本 3.2.0 在响应路径类调解器处获取请求有效负载内容
- javascript - 如何在 redux reducer 中更新 initialState
- r - 找出数据框列中的单元格是否包含 R 中的特定字符
- react-native - “Utils.startApp()”在 android 版本 10 和 11 中不起作用
- ios - Flutter app - Undefined symbols for architecture arm64 for iOS Device. Flutter Package - cryptlib
- flutter - 如何在颤动中调整画布对象/小部件的大小
- flutter - 将数据传递给提供者 - 在构建期间调用 setState() 或 markNeedsBuild()
- docker - 本地 Docker RabbitMQ - 连接超时