代码之家  ›  专栏  ›  技术社区  ›  Ofri

UICollectionViewCell复制

  •  1
  • Ofri  · 技术社区  · 6 年前

    CollectionView duplicate cell when loading more data

    所以我的情况是:

    1. 我在产品页面查看-删除产品
    2. 产品已从数据库中删除(成功)
    3. 在回调中,我从集合中删除已删除的产品
    4. 我刷新收藏
    5. 视图加载重复单元格

    我人生的开始 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()
                    }
                }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Shehata Gamal    6 年前

    既然你用了

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

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