UICollectionViewCell复制 [英] UICollectionViewCell duplication
问题描述
我知道之前已经有人问过这个问题,但是我的问题有所不同,遵循本指南并没有帮助:
I know this was asked here before already, but my issue is a bit different, and following this guide didn't help:
所以我的情况是:
- 我在ProductPageView上-正在删除产品
- 产品已成功从数据库中删除
- 解开塞古开火
- 在回调中,我从集合中删除了已删除的产品
- 我刷新收藏集
- 视图使用重复的单元格加载
我的cellForItemAtIndexPath
的开头:
Beginning of my cellForItemAtIndexPath
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "product_collection_cell", for: indexPath) as! ProductsCollectionViewCell
cell.ProductImageView.image = nil
cell.ProductName.text = nil
cell.ProductPrice.text = nil
cell.productUniqueID = nil
let prodInCell = searchActive ? filtered[indexPath.row] : products[indexPath.row]
let prodID = prodInCell.getUniqueID()
let dbRef = Storage.storage().reference().child(prodID).child("pic0.jpg")
cell.contentMode = .scaleAspectFit
cell.ProductImageView.image = #imageLiteral(resourceName: "DefaultProductImage")
dbRef.downloadURL(completion:
{
url, error in
if let error = error
{
print (error)
}
else if let url = url
{
cell.ProductImageView.loadImageUsingUrlString(urlString: url.absoluteString)
cell.ProductImageView.contentMode = UIViewContentMode.scaleToFill
cell.layoutIfNeeded()
}
})
cell.ProductImageView.clipsToBounds = true
//cell.ProductName = UILabel()
cell.ProductName.text = prodInCell.getName()
//cell.ProductPrice = UILabel()
cell.ProductPrice.text = String(prodInCell.getPrice())
cell.productUniqueID = prodInCell.getUniqueID()
return cell
}
我的展开回调函数:
@IBAction func unwindFromDeleteSegue(segue: UIStoryboardSegue)
{
if (prodToLoad == nil) {return}
for product in products
{
if product.getUniqueID() == prodToLoad?.getUniqueID()
{
products.remove(at: products.index(of: product)!)
ProductsCollection.reloadData()
break
}
}
}
我的numberOfItemsInSection
:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
if searchActive
{
return filtered.count
}
else
{
return products.count //return number of rows in section
}
}
有人可以指出我的错误源吗?
Can anyone please point me out to where the source of my bug might be ?
通知:
这种情况不仅在加载更多数据时发生,在集合中只有1个项目并且单元未被重用时也会发生
This doesn't occur only when loading more data, it also occurs when there is only 1 item in collection, and cells are not being reused
我注意到当注释此行时
StaticFunctions.ProductsStaticFunctions.removeProductFromDatabase(productUniqueID: self.productToDisplay.getUniqueID(), ownerID: self.productToDisplay.getOwnerID())
工作正常.当我取消评论时,它再次停止正常工作.
It works fine. When I un-comment it, it stops working properly again.
public static func removeProductFromDatabase(productUniqueID: String, ownerID: String)
{
let childUpdates = ["/\(Constants.TREE_A)/\(ownerID)/\(productUniqueID)/" : nil,
"/\(Constants.TREE_B)/\(ownerID)/\(productUniqueID)/" : nil,
"/\(Constants.TREE_C)/\(productUniqueID)/" : nil,
"/\(Constants.TREE_D)/\(productUniqueID)/" : nil,
"/\(Constants.TREE_E)/\(ownerID)/\(productUniqueID)/": nil,
"/\(Constants.TREE_F)/\(ownerID)/\(productUniqueID)/": nil,
"/\(Constants.TREE_G)/\(productUniqueID)/" : nil,
"/\(Constants.TREE_H/\(productUniqueID)/" : nil
] as [String : Any?]
Constants.refs.databaseRoot.updateChildValues(childUpdates)
}
我将产品添加到数组的方法:
My method for adding product to array:
ref?.observe(.value, with:
{ (snapshot) in
let allChildrenObjects = snapshot.children.allObjects
if allChildrenObjects.count == 0 {self.StopLoadingAnimation() ; return}
for child in allChildrenObjects as! [DataSnapshot]
{
// Code to execute when new product is added
let prodValueDictionary = child.value as? NSDictionary
if ((currentUserId == prodValueDictionary?[Constants.Products.ProductFields.PRODUCT_OWNER_ID] as? String) == itemsOfCurrentUser)
{
self.AddProductToCollection(productAsDictionary: prodValueDictionary, IsProductMine: itemsOfCurrentUser)
self.DispatchQueueFunc()
}
}
推荐答案
自使用以来
ref?.observe(.value, with:
{ (snapshot) in
您将有可能在调用此回调时对其进行每次编辑,这会导致意外结果,因为您遇到的麻烦就像在数组中复制数据一样,因此您需要进行一次观察
you'll be in threat that this callback is called every edit that occurs to it , which leads to an unexpected results as you encounter like duplicating data inside the array , so you need a single observe
ref?.observeSingleEvent(.value, with:
{ (snapshot) in
这篇关于UICollectionViewCell复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!