简体   繁体   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?

Here I am loading collection view data dynamically in table view cell and table view cells also created on dynamic json array count and here after selecting any element in collection view which is in first table view cell then the collection view needs to be reloaded with new data which is in second table view cell can anyone help me how to reload the collection view in second table view cell swift 3 if this is not possible can anyone provide me any alternative layout to implement this ?在这里,我正在表视图单元格中动态加载集合视图数据,并且表视图单元格也是在动态 json 数组计数上创建的,在这里在集合视图中选择第一个表视图单元格中的任何元素后,需要使用新数据重新加载集合视图这是第二个表格视图单元格中的任何人都可以帮助我如何在第二个表格视图单元格 swift 3 中重新加载集合视图,如果这是不可能的,任何人都可以向我提供任何替代布局来实现这一点吗?

Here is my cell for row method这是我的行方法的单元格

        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
        }

Here is my table view cell code这是我的表格视图单元格代码

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

here is my layout image这是我的布局图片

在此处输入图片说明

in didSelectItemAt of collection view, reload the table cell which contains size by doing在集合视图的 didSelectItemAt 中,通过执行重新加载包含大小的表格单元格

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

here value is position of row which you want to upate这里的是要更新的行的位置

Create a new variable "cellObj" which data type is same as you custom cell where you added collection view.创建一个新变量“cellObj”,其数据类型与添加集合视图的自定义单元格相同。

var cellObj:AddToCartCollectionTableViewCell!

After add assign cellObj value after this line.在此行后添加分配 cellObj 值后。

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

*** After doing you are able to reload collection view any where. *** 完成后,您可以在任何地方重新加载集合视图。

cellObj.collectionView.reloadData()

DispatchQueue.main.async { DispatchQueue.main.async {

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

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

相关问题 如何通过快速点击集合视图的单元格在表视图中重新加载数据 - How to reload data in table view by tapping the cell of collection view in swift 将数据从集合视图单元内的表视图单元传输到另一个集合视图单元(带有图片!) - Transfer data from table view cell inside a collection view cell, to another collection view cell [with picture!] 使用Swift在表视图中重新加载单元格数据 - reload cell data in table view with Swift 如何在Swift 3中实现表格视图单元格嵌入集合视图? - How to implement a table view cell embed collection view in swift 3? tableview单元格内的集合视图重新加载数据 - collection view inside tableview cell reload data 如何将数据从UI Table View传递到Table View Cell中的UI Collection View? - How to pass data from UI Table View to UI Collection View inside Table View Cell? 如何在表格视图单元格中创建动态集合视图,而不快速重新加载表格视图? - How to create dynamic collection view in a table view cell, without reloading table view swift? 表格视图单元格内的嵌套集合视图 swift iOS - Nested Collection View inside of table view cell swift iOS iOS Swift:自动调整大小的集合视图嵌套在表格视图单元格中 - iOS swift: autoresize collection view nested inside a table view cell 在表格视图单元格中添加集合视图 swift 3 - Adding a collection view inside a table view cell swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM