簡體   English   中英

在swift 3中單擊另一個表視圖單元格的集合視圖數據后,如何在表視圖單元格中重新加載集合視圖數據?

[英]How to reload the collection view data in table view cell after clicking another table view cell's collection view data in swift 3?

在這里,我正在表視圖單元格中動態加載集合視圖數據,並且表視圖單元格也是在動態 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)
    }
}

這是我的布局圖片

在此處輸入圖片說明

在集合視圖的 didSelectItemAt 中,通過執行重新加載包含大小的表格單元格

let indexpath = IndexPath(item: value, section: 0)
tableview.reloadRows(at: [indexpath], with: .none)

這里的是要更新的行的位置

創建一個新變量“cellObj”,其數據類型與添加集合視圖的自定義單元格相同。

var cellObj:AddToCartCollectionTableViewCell!

在此行后添加分配 cellObj 值后。

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

*** 完成后,您可以在任何地方重新加載集合視圖。

cellObj.collectionView.reloadData()

DispatchQueue.main.async {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM