繁体   English   中英

将数据从UITableView传递到UICollectionView

[英]Pass data from UITableView to UICollectionView

我想获得一个JSON响应并在我的viewcontroller中显示数据。

我有这个JSON响应:

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

我有块名称,描述和项目数组。 此外,所有项目都在这里传递关键“项目”

我创建了这个tableview类

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

现在我想在tableviewcell里面的collectionview中显示项目,但我不知道如何做到这一点。 由于每个块具有不同的项目数,我需要将块和项变量传递给我的CollectionViewClass,对吧? 但如何正确地做到这一点?

这是我的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
}

你可以试试

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM