0

Here I am having a layout in which a table view with dynamic rows and inside collection views for which to load cells inside it and here after selecting first collection view in first table view cell how to reload the second table view cell which is having quantity collection view and here all the collection views which created will be dynamically can any one help me how to resolve this or If not possible any alternative layout for this in swift 3 ?

Here is my cell for row at index path code

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

Here after selecting colour then depending on json data size array needs to be reloaded with new data

Vamsi S
  • 269
  • 3
  • 16
User
  • 73
  • 2
  • 11
  • I unable to get your question as bcz of weak statements, pls clarify what actually your requirement, so that we can only focus on that. – dahiya_boy Feb 09 '18 at 05:19
  • After selecting colour table view cell item in collection view for example 57 then I need to reload the second table view cell collection view items and need to load new sizes in collection view @dahiya_boy – User Feb 09 '18 at 05:21
  • if I select the 57 element then the sizes available in json is 176,177,178,179 that I need to load there @dahiya_boy – User Feb 09 '18 at 05:23
  • for 60 colour element all sizes are available – User Feb 09 '18 at 05:24
  • Here is my json url http://www.json-generator.com/api/json/get/cfuXTEdoZe?indent=2 @dahiya_boy – User Feb 09 '18 at 05:34
  • and here first array is `configurable_product_options` and after selecting colour then I need to filter from json data and reload the size data in collection view and the array for that is `configurable_product_options_stock` @dahiya_boy – User Feb 09 '18 at 05:36
  • I am writing the answer, it is little bit lengthy in post but simple and manageable in code side. Pls wait until I post my answer. – dahiya_boy Feb 09 '18 at 05:38
  • and here `configurable_product_options` is dynamic and now colour and size is coming it will increases in future also and so for that I placed a collection view and passing count dynamically @dahiya_boy – User Feb 09 '18 at 05:38

1 Answers1

0

Here I posted how to achieve the solution, follow the steps.

  1. CellForRow

Obviously your cellforrow must be like below,

if indexPath.row == 0{
   // your image cell
}
else if indexPath.row == 1{
   // color collection cell
}
else if indexPath.row == 2{
   // your size cell
}
  1. Now your collectionViewCell delegates and datasource must be bind to their TableViewcell i.e sizeCollectionCell must be binded with the sizetableCell and colorCollectionCell must be binded with the colortableCell.

  2. Now in colorCollectionCell here is the didSelect delegate called whenever user selects any color. Now use protocol-delegate pass these selected index to the mainVC class.

  3. Now your mainVC knows which color has been selected by the user, now reload only 2nd index of tableView like this

  4. Now focus on the cellForRow in the below condition

    if indexPath.row == 2{
       // pass your new array of sizes based ont he color selection to the collectionview here and reload your size collectionview.
    }
    

If still facing issue or need any help let me know.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Here u had defined colour and size statically but in json this may increase in future they said so now I am passing data dynamically it may be size or colour or any price label that I need it to be configurable how to pass dynamically @dahiya_bot – User Feb 09 '18 at 05:48
  • here I am passing table view no. of rows in section count dynamically – User Feb 09 '18 at 05:50
  • @User For your info, nothing is static in the answer. Instead of doing code I just write the procedure how to achieve the result. This pattern is the basic and simple. It manages your code in the better way. What make you to feel like it is static let me knoe, i will clear it to you. – dahiya_boy Feb 09 '18 at 05:50
  • in my json data `configurable_product_options` now u know that there is colour and size but later the arrays count increases from 2 to 3 or 4 then how to manage I am asking if after selecting colour then size array needs to be altered and if there is a chance of placing price also like that then how to place the layout @dahiya_boy – User Feb 09 '18 at 05:55
  • @user there will be one price of each product, you never get array of prices, keep in mind. Even I am not thinking your json structure is proper according to your requirement. – dahiya_boy Feb 09 '18 at 06:06
  • its like different pricing for different sizes as u have not seen in amazon or Flipkart apps – User Feb 09 '18 at 06:09
  • @user Dont forget corresponding to each color and size there will be one price. – dahiya_boy Feb 09 '18 at 06:12
  • if I get data like as shown in screen shot shown here then it need to display dynamically I am saying https://i.stack.imgur.com/xgVks.png – User Feb 09 '18 at 06:23
  • @user there is one price shown in the png that you added above in the comment not array of prices. – dahiya_boy Feb 09 '18 at 06:28
  • for example I told that but the problem is not that – User Feb 09 '18 at 06:33