我有以下 json 列表:
[
{
"title": "Languages",
"name_list": [
{
"title": "Hindi",
"locked": true
},
{
"title": "Hindi1",
"locked": true
},
{
"title": "Hindi2",
"locked": false
},
{
"title": "Hindi3",
"locked": true
},
{
"title": "Hindi4",
"locked": false
}],
},
{
"title": "Subject",
"name_list": [
{
"title": "Hindi4",
"locked": false
},
{
"title": "Hindi4",
"locked": true
},
{
"title": "Hindi4",
"locked": true
}]
}
]
我正在使用 TableView ,并且在 TableView 内我有 Collection View 。
现在,在我的 TableView 中,我将显示标题。在我的 Collection View 中,我将在每个 Collection View 单元格上显示 name_list
标题。现在一切正常。
我需要的是,正如您在 json 中看到的那样,有许多locked = true 和 false。
所以,在我的 TableView 中,哪个对象的locked == false
较少。我需要首先显示该标题。例如。
在我上面的json中,title = subject只有一个locked = false,但title = language有2个locked = false。因此,在我的表格 View 中,第一个主题必须显示和语言。
在每个 Collection View 中都是相同的,哪个对象已锁定= false,需要首先在我的 Collection View 单元格中显示。
请提供任何帮助。
这是我的表格 View 代码:
cellTitleLabel.text = jsonData?["title"] 为?字符串 ?? “”
在我的收藏 View 中:
if let gamesList = jsonData?["name_list"] as? [[String: Any]] {
let Info = nameList[indexPath.item]
collectionCell.cellTitleLabel.text = Info["title"] as? String ?? ""
}
var Info: [String: Any]?{
didSet {
cellTitleLabel.text = jsonData?["title"] as? String ?? ""
collectionView.reloadData();
guard let gamesList = jsonData?["name_list"] as? [[String: Any]] else {
return
}
}
}
最佳答案
我的解决方案是在 tableview 单元格中创建一个函数,并在 cellForRowAt indexPath 处使用相应的数组调用它。您只需通过cell 即可调用该函数。 fillCollectionView(数组)
。
class ExampleTableViewCell: UITableViewCell {
@IBOutlet weak var exampleCollectionView: UICollectionView!
var array = [[String:Any]]()
func fillCollectionView(with array: [[String:Any]]) {
self.array = array
exampleCollectionView()
}
关于ios - 如何根据我的json中的bool值过滤和显示 TableView 和 Collection View 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54664041/