首页 > 解决方案 > UICollectionViewCell 点击将无法正确注册

问题描述

我有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))
        }
   }

标签: iosswiftuicollectionviewuicollectionviewcell

解决方案


通过快速阅读您的代码,您不会在控制器上的任何地方存储 prodForPurchase 状态,然后重新加载数据。

重新加载后检查项目是否具有所需的状态。

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


推荐阅读