gpt4 book ai didi

ios - 将数据从 UITableView 传递到 UICollectionView

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:37 24 4
gpt4 key购买 nike

我想获得 JSON 响应并在我的 View Controller 中显示数据。

我有这个 JSON 响应:

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

我有带有名称、描述和项目数组的 block 。此外,所有项目都通过关键“项目”传递到这里

我已经创建了这个 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 中显示项目,但我不知道该怎么做。由于每个 block 都有不同的项目数,我需要将 block 和项目变量传递给我的 CollectionViewClass,对吧?但是如何正确地做到这一点呢?

这是我的 Collection View 类

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

关于ios - 将数据从 UITableView 传递到 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55736587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com