繁体   English   中英

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

[英]How to implement a table view cell embed collection view in swift 3?

在这里,我有一个布局,其中具有动态行的表视图和要在其中加载单元格的内部集合视图,在这里,在第一个表视图单元格中选择第一个集合视图之后,如何重新加载具有数量集合的第二个表视图单元格视图,这里创建的所有集合视图都是动态的,任何人都可以帮助我解决这个问题吗?或者,如果不可能的话,在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. CellForRow

显然,您的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 代表数据源必须结合其TableViewcellsizeCollectionCell必须与被绑定sizetableCellcolorCollectionCell必须与被绑定colortableCell

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

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

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

如果仍然面临问题或需要任何帮助,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM