gpt4 book ai didi

swift - UICollectionViewCell 着色不符合预期

转载 作者:行者123 更新时间:2023-11-28 13:59:08 25 4
gpt4 key购买 nike

我找不到答案,抱歉,如果之前有人问过这个问题。

我有一个 UICollectionView,我只是想更改单击的单元格的颜色。

但是,我的行为很奇怪。

我有三个项目,像这样:

enter image description here

假设每个 SELECTED 单元格都是彩色的,而 DESELECTED 不是

场景:1)我点击第一项:没有任何反应2)我再次点击第一项:现在已选择3)我点击第二项,它是SELECTED,第一个变为DESELECTED4)我再次点击第一个,没有任何反应。5)又是第一个:SELECTED6) 我点击第三个:第一个被取消

另一个场景:

1) 我点击第一项:没有任何反应2)我点击第二项:第一个被选中

这是怎么回事?

我的代码:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
// Display selected Item

let prodForPurchaseID = products[indexPath.row].getUniqueID()
let prodForPurchasePrice = products[indexPath.row].getPrice()

if (m_productsToPurchaseList[prodForPurchaseID] != nil)
{
// Product already marked for purchase. Need to remove it from purchase
changeCellColor(isMarkedAlready: true, didSelectItemAt: indexPath)
m_productsToPurchaseList.removeValue(forKey: prodForPurchaseID)
}
else
{
// Product not yet marked for purchase. Need to add it for purchase
changeCellColor(isMarkedAlready: false, didSelectItemAt: indexPath)
m_productsToPurchaseList[prodForPurchaseID] = prodForPurchasePrice
}
}

func changeCellColor(isMarkedAlready: Bool, didSelectItemAt indexPath: IndexPath)
{
let cell = ProductsCollection.cellForItem(at: indexPath)

if(isMarkedAlready)
{
// Need to unmark cell
cell?.backgroundColor = UIColor.clear
cell?.layer.borderColor = UIColor.black.cgColor
}
else
{
// Need to highlight cell
cell?.backgroundColor = UIColor.green
cell?.layer.borderColor = UIColor.yellow.cgColor
}
}

我的产品类:

class Product: NSObject
{
private var m_Name:String
private var m_Price: Double
private var m_Currency: String
private var m_Description: String
private var m_Location: String
private var m_PicturesURLs: [String]
private var m_OwnerID: String
private var m_OwnerDisplayName: String
//private var m_Amount: Int
private var m_CategoryID: String
private var m_Category: String
private var m_SaleTime: String?
private var m_ProductStatus: String
public var urlStr: String?
private var ID: String


public static let NEW_STATUS = "New"


init(name: String, price: Double, currency: String, description: String?, location: String, ownerID: String, ownerName: String, uniqueID: String, mainImageURL: String?, category: String!)
{
m_Name = name
m_Price = price
m_Currency = currency
m_Category = category
m_Description = ""
if let description = description
{
m_Description = description
}
m_Location = location
//m_Amount = amount?
m_ProductStatus = Product.NEW_STATUS
if (uniqueID == "")
{
ID = NSUUID().uuidString
}
else
{
ID = uniqueID
}

m_PicturesURLs = [String]()

m_OwnerID = ownerID
m_OwnerDisplayName = ownerName
m_CategoryID = "cat id"

if let mainImageURL = mainImageURL
{
m_PicturesURLs.append(mainImageURL)
}
}


public func setUrlStr(str: String)
{
urlStr = str
}

public func getCategoryID() -> String
{
return m_CategoryID
}

public func getCategory() -> String
{
return m_Category
}

public func getCurrency() -> String
{
return m_Currency
}

public func getLocation() -> String
{
return m_Location
}

public func getSaleTime() -> String?
{
return m_SaleTime
}

public func getProductStatus() -> String
{
return m_ProductStatus
}

public func getUniqueID() -> String
{
return ID
}

public func getName() -> String
{
return m_Name
}

public func getPrice() -> Double
{
return m_Price
}

public func getDescription() -> String
{
return m_Description
}

public func getImages() -> [String]
{
return m_PicturesURLs
}

public func getOwnerID() -> String
{
return m_OwnerID
}

public func getOwnerName() -> String
{
return m_OwnerDisplayName
}

public func AddImageURLToProduct(URL url: String)
{
m_PicturesURLs.append(url)
}

public func getMainImageURLString() -> String
{
if let mainImageURL = m_PicturesURLs.first
{
return mainImageURL
}
return ""
}

public func getNumberOfImages() -> Int
{
return m_PicturesURLs.count
}
}

CellForItemAt 函数:

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()
return cell
}

最佳答案

将新属性添加到您的 Product

var isMarked: Bool = false

首先将这段代码添加到cellForItemAt数据源方法

cell.backgroundColor = prodInCell.isMarked ? UIColor.green : UIColor.clear
cell.layer.borderColor = prodInCell.isMarked ? UIColor.yellow.cgColor : UIColor.black.cgColor

然后在 CollectionView 中 didSelectItemAt 委托(delegate)方法 toogle 所选项目的 isMarked 属性的值并在 Collection View 中重新加载数据

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
...
products[prodForPurchaseID].toogle()
collectionView.reloadData()
...
}

关于swift - UICollectionViewCell 着色不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53683880/

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