【问题标题】:didSelectItemAt item by default size collection view cell is called instead Color Collection View CelldidSelectItemAt 项目默认大小集合视图单元被称为颜色集合视图单元
【发布时间】:2018-12-27 10:12:41
【问题描述】:

当时我应该调用颜色集合视图单元格并在索引路径处选择项目项..

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

请帮我解决这个问题。

【问题讨论】:

  • 从代码中我了解到您有 2 个不同的集合视图,您在 cellForItemAt 中进行了区分,但您忘记在 didSelectItemAt 中进行区分。
  • didSelectItemAt中如何区分,请解决这个问题?
  • 当您比较 cellForItemAt 中的 collectionView 对象时,与 didSelectItemAt indexPath 中的比较方式相同。

标签: ios iphone swift3 uicollectionviewcell


【解决方案1】:
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     
       }
    }
}

【讨论】:

    【解决方案2】:

    尝试像这样更改 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 提供委托和数据源。

    【讨论】:

    • 你面临什么问题?那个方法叫什么?放置断点并打印 collectionView 对象
    • 这个方法只对size collection调用,另外一个方法就是color collection不调用,color collection有图片。什么是问题?请帮助我
    • 你给两个collectionview的delegate和dataSource了吗?
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多