UICollectionViewCell点击将无法正确注册

时间:2018-12-09 18:48:03

标签: ios swift uicollectionview uicollectionviewcell

我有一个UICollectionView,其中有一些单元格。

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

行为:

如果我具有单元格A和单元格B,并且单​​击了单元格A,然后单击了单元格B,则单元格B上的单击注册为对单元格A的单击。再次单击单元格B上的单击,然后在单元格B上进行了正确注册。

因此,一旦视图加载被忽略,首先单击ANY单元。 如果我之前在单元格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 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

您尚未发布isProductMarked() func的实现。它的值似乎未更新。不要忘记在点击时更新产品标记的标志。