首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell复制

UICollectionViewCell复制
EN

Stack Overflow用户
提问于 2018-09-11 04:38:45
回答 1查看 591关注 0票数 1

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

CollectionView duplicate cell when loading more data

所以我的情况是:

  1. 我在ProductPageView上-删除产品
  2. 从数据库中删除产品(成功)
  3. 松开段点火
  4. 在回调中,我从集合中删除已删除的产品。
  5. 我刷新收集
  6. 使用重复单元格加载视图

我的cellForItemAtIndexPath开始

代码语言:javascript
运行
复制
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
}

我的解压回调功能:

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        if searchActive
        {
            return filtered.count
        }
        else
        {
            return products.count    //return number of rows in section
        }
    }

有人能告诉我我的窃听器的源头在哪里吗?

公告:

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

编辑:

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

代码语言:javascript
运行
复制
StaticFunctions.ProductsStaticFunctions.removeProductFromDatabase(productUniqueID: self.productToDisplay.getUniqueID(), ownerID: self.productToDisplay.getOwnerID())

效果很好。当我不评论它时,它又停止正常工作了。

代码语言:javascript
运行
复制
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)
    }

我将产品添加到数组中的方法:

代码语言:javascript
运行
复制
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()
                }
            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-13 10:01:09

既然你用

代码语言:javascript
运行
复制
ref?.observe(.value, with:
        { (snapshot) in

您将面临这样的威胁:此回调将被调用它发生的每一个编辑,这将导致意外的结果,因为您会遇到类似在数组中复制数据的情况,因此需要一个单独的观察。

代码语言:javascript
运行
复制
ref?.observeSingleEvent(.value, with:
        { (snapshot) in
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52268758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档