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

UICollectionViewCell单击将无法正确注册

  •  0
  • Ofri  · 技术社区  · 7 年前

    UICollectionView 有一些细胞。

    我有一个奇怪的错误,点击一个单元格有时注册为点击错误的单元格。

    行为:

    如果我有单元格A和单元格B,然后单击单元格A和单元格B,那么单击单元格B会注册为单击单元格A。再次单击单元格B会正确注册到单元格B。

    根据这一点,在忽略视图加载后,首先单击任何单元格。 如果我在之前单击单元格B之后再单击单元格A两次,结果是单元格B被单击一次,单元格A被单击一次。

    从另一个角度来看:

    从另一个角度来看:

    每次单击都会在先前单击的单元格中注册。

    我的班级:

    class SelectCells: ProductsTableViewController
    {
    
        var m_productsToPurchaseList : [String : Double] = [:]
        var m_sellerID = ""
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            self.LoadProductsByUserID(productsToShow: Constants.Products.ProductTrees.MY_SALES, UserID: m_sellerID) // My sales is all sales in contact perspective
    
        }
    
        override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
        {
            // Display selected Item
    
            print(indexPath.item)
            print(indexPath.section)
    
            let prodForPurchase = products[indexPath.row]
            let prodForPurchaseID = prodForPurchase.getUniqueID()
    
            prodForPurchase.toggleProductSelected()
    
            if (prodForPurchase.isProductMarked())
            {
                // Product not yet marked for purchase. Need to add it for purchase
                m_productsToPurchaseList[prodForPurchaseID] = prodForPurchasePrice
            }
            else
            {
                // Product already marked for purchase. Need to remove it from purchase
                m_productsToPurchaseList.removeValue(forKey: prodForPurchaseID)
            }
    
            ProductsCollection.reloadData()
    
        }        
    }
    

    超类函数:

    extension ProductsCollectionViewController: UICollectionViewDataSource
    {
    
        func createCollectionViewCell(_ 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()
            cell.contentMode = .scaleAspectFit
    
            if let str = prodInCell.urlStr
            {
                cell.ProductImageView.sd_setImage(with: URL(string:str), placeholderImage: #imageLiteral(resourceName: "DefaultProductImage"))
            }
            else
            {
                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
                        {
                            Constants.logger.error(error)
                        }
                        else if let url = url
                        {
                            prodInCell.setUrlStr(str: url.absoluteString)  // store for upcoming need
                            cell.ProductImageView.sd_setImage(with: URL(string:url.absoluteString), placeholderImage: #imageLiteral(resourceName: "DefaultProductImage"))
                            cell.ProductImageView.contentMode = UIViewContentMode.scaleToFill
                            cell.layoutIfNeeded()
                        }
                })
    
            }
            cell.ProductImageView.clipsToBounds = true
            cell.ProductName.text = prodInCell.getName()
            cell.ProductPrice.text = String(prodInCell.getPrice())
            cell.productUniqueID = prodInCell.getUniqueID()
    
            let isProductMarked : Bool = prodInCell.isProductMarked()
    
            cell.backgroundColor = isProductMarked ? UIColor.green : UIColor.clear
            cell.layer.borderColor = isProductMarked ? UIColor.yellow.cgColor : UIColor.black.cgColor
    
            return cell
        }
    
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {
            return createCollectionViewCell(collectionView, cellForItemAt: indexPath)
        }
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
        {
            // Display selected Item
            prodToLoad = products[indexPath.row]
            performSegue(withIdentifier: "view_product_information", sender:self  )
        }
    
    
    
        // Swift 3.0
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
        {
            return GetViewCGSize(collectionView)
        }
    
    // This function was created so that we can override it for different views that are ProductsCollectionView to have cells look different
        func GetViewCGSize(_ collectionView: UICollectionView) -> CGSize
        {
            return CGSize(width: CGFloat((collectionView.frame.size.width / 3) - 20), height: CGFloat(100))
            }
       }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Dimitrios Kalaitzidis    7 年前

    重新加载后检查项目是否处于所需状态。

    另外,尝试从cellForItem中删除代码并在cell类上实现它。

        2
  •  0
  •   Timur Bernikovich    7 年前

    您尚未发布的实现 isProductMarked() 功能。它的值似乎没有更新。别忘了在点击时更新产品标记标志。

    推荐文章