在选择索引Path的项目时,我应该已经调用了颜色收集视图单元。
extension ProductDetailViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if( collectionView .isEqual(sizeCollection))
{
return (size[section].product_option_value!.count)
}
else
{
return (colorArray[section].product_option_value!.count)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView .isEqual(sizeCollection))
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sizecell", for: indexPath) as! SizeCollectionViewCell
cell.sizeLabel.text = size[indexPath.section].product_option_value?[indexPath.item].name
return cell
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorcell", for: indexPath) as! ColorCollectionViewCell
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
if let imageString = self.products[indexPath.section].images?[indexPath.item].thumb
{
if let imageUrl = URL(string:imageString)
{
if let imageData = try? Data(contentsOf: imageUrl)
{
DispatchQueue.main.async {
cell.colorTypesOfImages.image = UIImage(data: imageData)
cell.colorLabel.text = self.colorArray[indexPath.section].product_option_value?[indexPath.item].name
}
}}}}
return cell
}
}
}
extension ProductDetailViewController:UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if let imageString = self.products[indexPath.section].images?[indexPath.item].popup
{
if let imageUrl = URL(string:imageString)
{
if let imageData = try? Data(contentsOf: imageUrl)
{
DispatchQueue.main.async {
self.productOriginalImage.image = UIImage(data: imageData)
}
}}}
}
请帮助我解决此问题。
答案 0 :(得分:0)
extension ProductDetailViewController:UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if (collectionView .isEqual(sizeCollection))
{
// put you login for sizeCollection
}
else
{
// put you login for other collection
}
}
}
答案 1 :(得分:0)
尝试更改类似这样的didSelectItemAt代码
extension ProductDetailViewController:UICollectionViewDelegate
{
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
if (collectionView .isEqual(sizeCollection))
{
//sizeCollection
}
else {
if let imageString = self.products[indexPath.section].images?[indexPath.item].popup
{
if let imageUrl = URL(string:imageString)
{
if let imageData = try? Data(contentsOf: imageUrl)
{
DispatchQueue.main.async {
self.productOriginalImage.image = UIImage(data: imageData)
}
}}}
}
}
}
注意:请确保同时为两个collectionview提供委托和dataSource。