我知道这里已经问过这个问题了,但我的问题有点不同,遵循这个指南没有帮助:
CollectionView duplicate cell when loading more data
所以我的情况是:
我的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
}
}
有人能告诉我我的窃听器的源头在哪里吗?
公告:
这不仅在加载更多数据时发生,而且在集合中只有一个项且单元格未被重用时也会发生
编辑:
我注意到,当这一行在评论中时:
StaticFunctions.ProductsStaticFunctions.removeProductFromDatabase(productUniqueID: self.productToDisplay.getUniqueID(), ownerID: self.productToDisplay.getOwnerID())
效果很好。当我不评论它时,它又停止正常工作了。
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)
}
我将产品添加到数组中的方法:
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()
}
}
发布于 2018-09-13 10:01:09
既然你用
ref?.observe(.value, with:
{ (snapshot) in
您将面临这样的威胁:此回调将被调用它发生的每一个编辑,这将导致意外的结果,因为您会遇到类似在数组中复制数据的情况,因此需要一个单独的观察。
ref?.observeSingleEvent(.value, with:
{ (snapshot) in
https://stackoverflow.com/questions/52268758
复制相似问题