如何在swift 3中单击另一个表格视图单元格的集合视图数据后,在表格视图单元格中重新加载集合视图数据?

时间:2018-02-13 09:11:29

标签: ios uitableview swift3 uicollectionview

这里我在表视图单元格和表视图单元格中动态加载集合视图数据也是在动态json数组计数上创建的,这里在选择第一个表视图单元格中的集合视图中的任何元素之后,需要重新加载集合视图使用第二个表视图单元格中的新数据任何人都可以帮助我如何在第二个表视图单元格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 indexPath.item == 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 = selectedValue
                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
        print(self.selectedIndex)
        self.collectionView.reloadData()
        self.sizeArray.removeAll()
        self.selectedValue = configurableProduct?.extensionAttribute?.productOptions[1].values[indexPath.item].valueIndex
        self.getCurrentRow = 1
        self.collectionView.reloadData()
    print(self.selectedValue)
    }
    else {
        print(collectionView.tag)
    }
}

这是我的布局图片

enter image description here

3 个答案:

答案 0 :(得分:1)

在集合视图的didSelectItemAt中,通过执行

重新加载包含大小的表格单元格
let indexpath = IndexPath(item: value, section: 0)
tableview.reloadRows(at: [indexpath], with: .none)

此处是您想要提升的行的位置

答案 1 :(得分:0)

创建一个新变量“cellObj”,其数据类型与您添加集合视图的自定义单元格相同。

var cellObj:AddToCartCollectionTableViewCell!

在此行之后添加assign cellObj值。

let cell = addToCartTableView.dequeueReusableCell(withIdentifier: "addToCartCollectionCell") as! AddToCartCollectionTableViewCell
cellObj = cell

***完成后,您可以在任何地方重新加载集合视图。

cellObj.collectionView.reloadData()

答案 2 :(得分:0)

DispatchQueue.main.async {

           // cell!.tableView.reloadData()
            collectionView.reloadItems(at: [indexPath])
            cell!.tableView.reloadData()
        }
           })