gpt4 book ai didi

ios - UICollectionViewCell 点击不会正确注册

转载 作者:行者123 更新时间:2023-11-28 11:40:54 30 4
gpt4 key购买 nike

我有一个包含一些单元格的 UICollectionView

我有一个奇怪的错误,点击一个单元格有时会显示为点击了错误的单元格。

行为:

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

据此,一旦 View 加载被忽略,首先单击任何单元格。如果我之前点击单元格 B 后又点击单元格 A 两次,则结果是单元格 B 被点击一次,单元格 A 被点击一次。

另一种看待它的方式:

每当我点击一个单元格而前一次点击是在一个不同的单元格上时,新的点击记录在前一个单元格上。

另一种看待它的方式:

每次点击都记录在之前点击的单元格上。

我对此感到困惑。有什么帮助吗?

我的类(class):

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()

}
}

父类(super class)的函数:

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

最佳答案

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

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

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

关于ios - UICollectionViewCell 点击不会正确注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53695548/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com