3

I am created UICollectionView inside of UITableView ,populating array values in UItableview is working perfect, when trying to populate a array values into collectionView getting same values in collectionView of all the rows of tableView, I want to populate a zeroth index array in collectionview of zeroth row UItableview and so on below I tried a sample,

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource{


        let array = ["product 1","product 2","product 3","product4"]
        let array1 = ["product id 1","product id 2","product id 3","product id 4"]
        let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]] as [NSArray]


     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
            cell.tableviewlabel1.text = array[indexPath.row]
             cell.tableviewlabel2.text = array1[indexPath.row]
            return cell
        }
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return array2.count
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection", for: indexPath) as! CollectionViewCell


            let rowValue : NSArray = array2[indexPath.row]
            for i in 0..<rowValue.count {
            cell.collectionviewlabel1.text = rowValue[i] as? String
            }
            return cell

        }

For example ,I want a zeroth index of array2 values in collectionview of zeroth row of tableview, and first index values in collectionview of first row of tableview and so on.

Kavitha Madhu
  • 396
  • 1
  • 5
  • 19
  • Is your collection view is inside tableviewcell? – Wings Oct 11 '18 at 10:16
  • 1
    Your collection view data source methods should be in your cell class, not in your view controller. – Paulw11 Oct 11 '18 at 10:18
  • yeah, I implemented my collectionview inside of uitableviewcell – Kavitha Madhu Oct 11 '18 at 10:18
  • 1
    then follow the @Paulw11 comment it must be inside in your tableview cell – Wings Oct 11 '18 at 10:19
  • Yes, but your cell class must implement `UICollectionViewDataSource`, not your view controller – Paulw11 Oct 11 '18 at 10:20
  • yeah, thank you..can u suggest some sample for this @ Paulw11 and V_rohit – Kavitha Madhu Oct 11 '18 at 10:21
  • Preferred below link :-https://stackoverflow.com/questions/31582378/ios-8-swift-tableview-with-embedded-collectionview or https://medium.com/@aestusLabs/adding-a-uicollectionviews-to-a-custom-uitableviewcell-xib-tutorial-swift-4-xcode-9-2-1ec9ce4095d3 – V D Purohit Oct 11 '18 at 11:49
  • @KavithaMadhu please note that [I rejected your suggested edit](https://stackoverflow.com/review/suggested-edits/21145653) because you should "_Use '#selector' instead of explicitly constructing a 'Selector'_". (this is an Xcode warning) – Cœur Oct 17 '18 at 03:00

2 Answers2

2

First your array2 has to be like this :

let array2 = [["product id 1","product id 2"],["product id 3","product id 4"],["product id 5","product id 6"],["product id 7","product id 8","product id 9","product id 10","product id 11"]]

Don't use NSArray in Swift. you can write it as [String], [[String]].

Your ViewController will have only UITableViewDelegate and UITableViewDatasource methods:

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cells") as! TableViewCell
        cell.tableviewlabel1.text = array[indexPath.row]
        cell.tableviewlabel2.text = array1[indexPath.row]

        cell.arrayForCollectionView = array2
        return cell
    }
}

Then in your TableViewCell:

class TableViewCell: UITableViewCell {

    var arrayForCollectionView : [[String]]! {
        didSet {
            self.collectionView.reloadData
        }
    }

    @IBOutlet weak var collectionView: UICollectionView!

}

extension TableViewCell : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (arrayForCollectionView != nil) {
            return arrayForCollectionView.count
        }
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection", for: indexPath) as! CollectionViewCell


        let rowValue = arrayForCollectionView[indexPath.row]
        for i in 0..<rowValue.count {
            cell.collectionviewlabel1.text = rowValue[i] as? String
        }
        return cell
    }
}

Let me know if you are getting any issue.

Amit
  • 4,837
  • 5
  • 31
  • 46
  • getting Cannot subscript a value of type '[[String]]!' error. in let rowValue : NSArray = arrayForCollectionView[indexPath.row] – Kavitha Madhu Oct 11 '18 at 10:49
  • Sorry forgot to remove `NSArray`. – Amit Oct 11 '18 at 10:58
  • Hello Amit, I didn't get what I asked ...In my collection view getting product id 2,product id 4,product id 6,product id 11 and all the rows of uitableview – Kavitha Madhu Oct 11 '18 at 11:10
  • Thanks Amit ..I just edited in this line "cell.arrayForCollectionView = array2" as " "cell.arrayForCollectionView = array2[indexpath.row]" ..its working perfect ,thanks for ur contribution – Kavitha Madhu Oct 11 '18 at 12:31
1

Giving an example for that:

first tableView is inside ViewController

 class ViewController: UIViewController {

//MARK:- IBOUTELTS
//MARK:-
@IBOutlet weak var tableView: UITableView!

//MARK:- VARIABLES
//MARK:-

override func viewDidLoad() {
    super.viewDidLoad()


}


}

extension YourViewConroller: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {


}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


}

}

And Your CollectionView is inside TableViewCell

 extension YourTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {


}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {



}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}


}

And make different class for TableView and CollectionView:

 class YourTableViewCell: UITableViewCell { // tableview cell

}


 classYourCollectionViewCell: UICollectionViewCell { // collectionviewcell

  }
Wings
  • 2,398
  • 23
  • 46