ios - 如何根据我的json中的bool值过滤和显示 TableView 和 Collection View 值

标签 ios swift iphone filter

我有以下 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
            }
        }
    }

enter image description here

最佳答案

我的解决方案是在 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/

相关文章:

iphone - 有没有办法自定义 iPhone 上现有的联系人应用程序?

ios - 我无法在 MagicalRecord 中找到 FirstByAttribute

ios - 通过 prepare for segue 传递数据时出错

swift - 在 Swift 3 中实现教堂数字时出现非转义错误

iphone - 为什么我的 Objective-C 类实例变量没有被设置?

iphone - 将现有 Drupal 页面与 PhoneGap iOS 应用程序连接

ios - 在 Realm 中存储值

ios - 如何为 iOS 本地通知内容提供自定义辅助功能文本 - UNNotificationContent

iphone - 模拟一个水龙头,可以吗?

iphone - iOS Facebook SDK,如何在App内登录,而不是在safari中登录