Bobby Redjeans
Bobby Redjeans

Reputation: 599

Pass data from UITableView to UICollectionView

I want to get a JSON response and display data in my viewcontroller.

I have this JSON response:

{"status":1,"data":{"blocks":[{"name":"CustomBlock","description":"CustomDescription","items":[1]}], "items":[{"id:"1", name: "testgame"}]}

I have blocks with name, description, and array of items. Also, all items is passing here with the key "items"

I have created this tableview class

BlocksTableView.swift

class BlocksTableView : UITableView, UITableViewDataSource, 
UITableViewDelegate
{
    var blocks = [Block]()
    var items : BlockItem!

    override func awakeFromNib() {
        self.delegate = self
        self.dataSource = self
        self.loadBlocks()
    }
    func loadBlocks()
    {
        guard let url = URL(string : "myURL") else { return }
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }
            do {
                let response = try JSONDecoder().decode(APIResponse.self, from: data)
                self.blocks = response.data.blocks;
                self.items = response.data.items;

                DispatchQueue.main.async {
                    self.reloadData()
                }
            } catch let jsonErr {
                print(jsonErr)
            }
            }.resume()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return blocks.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "BlockCell") as? BlockCell else { return UITableViewCell() }
        cell.blockName.text = blocks[indexPath.row].name
        cell.blockDescription.text = blocks[indexPath.row].description
        cell.gameCollectionView.reloadData()
        return cell
    }
}

Now I want to display items in collectionview inside tableviewcell, but I have no idea how to do this. Since each block has different count of items, I need to pass blocks and items variables to my CollectionViewClass, right? But how to do this properly?

Here is my collectionviewclass

class GameCollectionView : UICollectionView, 
UICollectionViewDataSource, UICollectionViewDelegate
{
    override func awakeFromNib() {
        self.delegate = self
        self.dataSource = self
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //here i need to return number of items in current block, something like blocks[0].items.count
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCollectionCell", for: indexPath) as? GameCollectionCell else { return
            UICollectionViewCell()
    }
    return cell
}

Upvotes: 1

Views: 236

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can try

class BlocksTableView : UITableView, UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate {

    var blocks = [Block]()
    var items : BlockItem!

    override func awakeFromNib() {
        self.delegate = self
        self.dataSource = self
        self.loadBlocks()
    }
    func loadBlocks()
    {
        guard let url = URL(string : "myURL") else { return }
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else { return }
            do {
                let response = try JSONDecoder().decode(APIResponse.self, from: data)
                self.blocks = response.data.blocks;
                self.items = response.data.items;

                DispatchQueue.main.async {
                    self.reloadData()
                }
            } catch let jsonErr {
                print(jsonErr)
            }
            }.resume()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return blocks.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "BlockCell") as? BlockCell else { return UITableViewCell() }
        cell.blockName.text = blocks[indexPath.row].name
        cell.blockDescription.text = blocks[indexPath.row].description
        cell.gameCollectionView.delegate = self
        cell.gameCollectionView.dataSource = self
        cell.gameCollectionView.tag = indexPath.row
        cell.gameCollectionView.reloadData()
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return  blocks[collectionView.tag].items.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCollectionCell", for: indexPath) as? GameCollectionCell else { return
            UICollectionViewCell() } 

      // here use  blocks[collectionView.tag].items[indexPath.row] for each item
        return cell
    }
}

Upvotes: 3

Related Questions