如何在swift 3中实现表格视图单元格嵌入集合视图?

时间:2018-02-09 05:01:35

标签: ios uitableview swift3 uicollectionview

这里我有一个布局,其中有一个动态行和内部集合视图的表视图,在其中加载单元格,在这里,在第一个表视图单元格中选择第一个集合视图之后如何重新加载第二个表视图单元格有数量集合视图,这里创建的所有集合视图都将是动态的,任何人都可以帮助我解决这个问题,或者如果不可能在swift 3中有任何替代布局?

这是索引路径代码

行的单元格
     if indexPath.section == 0 {
           let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartCollectionCell") as! AddToCartCollectionTableViewCell
            cell.configurableProduct = self.detailModel
            print(self.detailModel)
            cell.collectionView.tag = indexPath.row
            self.addToCartTableView.setNeedsLayout()
            self.addToCartTableView.layoutIfNeeded()
            cell.collectionView.reloadData()
            cell.cellLabel.text = detailModel?.extensionAttribute?.productOptions[indexPath.row].label
            if detailModel?.extensionAttribute?.productOptions[indexPath.row].label == "Size"{
                cell.sizeGuideBtn.isHidden = false
            }else{
                cell.sizeGuideBtn.isHidden = true
            }
            cell.getCurrentRow = indexPath.row
            return cell
        }else {
            let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartQtyCell") as! AddToCartQuantityTableViewCell
            self.addToCartTableView.setNeedsLayout()
            self.addToCartTableView.layoutIfNeeded()
            cell.QtyLabel.text = "Qty"
            return cell
        }

这是我的表格视图单元格代码

override func awakeFromNib() {
        super.awakeFromNib()
        collectionView.delegate = self
        collectionView.dataSource = self
        print(getCurrentRow)
        // Initialization code
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       return CGSize(width: 50, height: 30)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print(configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values.count)
        return (configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values.count)!
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! AddToCartCollectionViewCell
        if getCurrentRow == 0 {
            let items = configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values[indexPath.row]
            cell.collectionLabel.text =  "\(items?.valueIndex as! Int)"
            if indexPath.item == self.selectedIndex{
                cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
            }else if self.selectedIndex == nil {
                cell.backgroundColor = UIColor.white
            }else{
                cell.backgroundColor = UIColor.white
            }
        }
        else {
            if selectedValue != nil {
                for item in (self.configurableProduct?.extensionAttribute?.productStock)! {
                    //                            let jsonStr = "{\"label\":\"57-175\",\"stock\":0}"
                    let dict = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!, options: []) as! [String:Any]
                    let labelValue = dict["label"] as! String
                    print(labelValue)
                    let values:[String] = labelValue.components(separatedBy: "-")
                    print(values)
                    self.colorNumber = Int(values[0])
                    self.sizeNumber = Int(values[1])
                    let stock = dict["stock"] as! Int
                    let value = selectedIndex
                    if value == self.colorNumber {
                        if stock != 0 {
                            self.sizeArray.append(self.sizeNumber!)
                            print(self.sizeArray)
                            cell.collectionLabel.text =  "\(self.sizeNumber)"
                        }
                    }
                }
                if indexPath.item == self.selectedIndex{
                    cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
                }else if self.selectedIndex == nil {
                    cell.backgroundColor = UIColor.white
                }else{
                    cell.backgroundColor = UIColor.white
                }
            }
            else {
                let items = configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values[indexPath.item]
                print(items?.valueIndex)
                for item in (self.configurableProduct?.extensionAttribute?.productStock)! {
                    //                            let jsonStr = "{\"label\":\"57-175\",\"stock\":0}"
                    let dict = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!, options: []) as! [String:Any]
                    let labelValue = dict["label"] as! String
                    print(labelValue)
                    let values:[String] = labelValue.components(separatedBy: "-")
                    print(values)
                    self.colorNumber = Int(values[0])
                    self.sizeNumber = Int(values[1])
                    let stock = dict["stock"] as! Int
                    let value = self.selectedIndex
                    if value == self.colorNumber {
                        if stock != 0 {
                            self.sizeArray.append(self.sizeNumber!)
                            print(self.sizeArray)
                            cell.collectionLabel.text =  "\(items?.valueIndex as! Int)"
                        }
                    }else {
                        cell.collectionLabel.text =  "\(items?.valueIndex as! Int)"
                    }
                }
                if indexPath.item == self.selectedIndex{
                    cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
                }else if self.selectedIndex == nil {
                    cell.backgroundColor = UIColor.white
                }else{
                    cell.backgroundColor = UIColor.white
                }
            }
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView.tag == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! AddToCartCollectionViewCell
            cell.backgroundColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
            cell.collectionLabel.layer.cornerRadius = 15
            cell.collectionLabel.layer.borderColor = #colorLiteral(red: 0.007509540026, green: 0.6581087804, blue: 0.01165772038, alpha: 1)
            self.dataSelected = true
            self.selectedIndex = indexPath.item
            if collectionView.tag == 0 {
                collectionView.tag = 1
                self.collectionView.reloadData()
                self.sizeArray.removeAll()
                self.getCurrentRow = 1
                self.selectedValue = configurableProduct?.extensionAttribute?.productOptions[getCurrentRow].values[indexPath.item].valueIndex
            }
            else {

            }
        }else {
            print(collectionView.tag)
        }
    }

在选择颜色后,根据json数据大小,数组需要重新加载新数据

1 个答案:

答案 0 :(得分:0)

在这里,我发布了如何实现解决方案,请按照步骤进行操作。

  1. CellForRow
  2. 显然,您的cellforrow必须如下,

    if indexPath.row == 0{
       // your image cell
    }
    else if indexPath.row == 1{
       // color collection cell
    }
    else if indexPath.row == 2{
       // your size cell
    }
    
    1. 现在,您的collectionViewCell 代理数据源必须绑定到他们的TableViewcell,即sizeCollectionCell必须绑定sizetableCellcolorCollectionCell必须与colortableCell绑定。

    2. 现在在colorCollectionCell中,只要用户选择任何颜色,就会调用didSelect委托。现在使用protocol-delegate将这些选定的索引传递给mainVC类。

    3. 现在您的mainVC知道用户选择了哪种颜色,现在只重新加载tableView的第二个索引,如this

    4. 现在关注以下条件中的cellForRow

      if indexPath.row == 2{
         // pass your new array of sizes based ont he color selection to the collectionview here and reload your size collectionview.
      }
      
    5. 如果仍然面临问题或需要任何帮助,请告诉我。