gpt4 book ai didi

swift - UICollectionViewCell 复制

转载 作者:行者123 更新时间:2023-11-28 05:52:55 26 4
gpt4 key购买 nike

我知道之前已经有人在这里问过这个问题,但我的问题有点不同,遵循本指南没有帮助:

CollectionView duplicate cell when loading more data

所以我的情况是:

  1. 我在 ProductPageView - 删除产品
  2. 从数据库中删除产品(成功)
  3. Unwind Segue 启动
  4. 在回调中,我从集合中删除已删除的产品
  5. 我刷新收藏
  6. 查看包含重复单元格的负载

我的 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
}
}

任何人都可以指出我的错误来源可能在哪里吗?

注意事项:

这不仅在加载更多数据时发生,在集合中只有 1 项且单元格未被重用时也会发生

编辑:

我注意到当这一行在评论中时:

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()
}
}

最佳答案

自从你使用

ref?.observe(.value, with:
{ (snapshot) in

你会面临这样的威胁,即每次对其进行编辑时都会调用此回调,当你遇到像在数组中复制数据时会导致意外结果,因此你需要一次观察

ref?.observeSingleEvent(.value, with:
{ (snapshot) in

关于swift - UICollectionViewCell 复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com