在同一数组的不同索引处使用 objects 填充 CollectionView 和 TableView

Populate CollectionView and TableView using objects at different index of same array

我想在 CollectionView 中显示 "group_type" == 1 的产品,在 TableView 中显示 "group_type" == 2 的产品。

我想按顺序填充 TableView,其中 "group_title" 将作为节 Header 和数组 "Products" 中的产品作为行。 下面是我的 JSON.

我需要什么 Swift 代码?

  "product_groups": [
    {
        "group_title": "Recommended",
        "group_type": 1,
        "products": [
            {
                "product_id": 1,
                "product_name": "Product 1",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 2,
                "product_name": "Product 2",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 3,
                "product_name": "Product 3",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 4,
                "product_name": "Product 4",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 5,
                "product_name": "Product 5",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 6,
                "product_name": "Product 6",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            },
            {
                "product_id": 7,
                "product_name": "Product 7",
                "product_price": "Rs 1,999/-",
                "product_category": "Equipment & Chairs",
                "product_image": "https://project-isdental-cammy92.c9users.io/api/images/products/product_1.jpg",
                "product_description": "Description 1"
            }
        ]
    },
    {
        "group_title": "Offers",
        "group_type": 2,
        "products": [
            {
                "product_id": 8,
                "product_name": "Product 8",
                "product_price": "Rs 1,999/-",
                "product_category": "Materials & Consumables",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 9,
                "product_name": "Product 9",
                "product_price": "Rs 1,999/-",
                "product_category": "Materials & Consumables",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 10,
                "product_name": "Product 10",
                "product_price": "Rs 1,999/-",
                "product_category": "Materials & Consumables",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 11,
                "product_name": "Product 11",
                "product_price": "Rs 1,999/-",
                "product_category": "Materials & Consumables",
                "product_image": "",
                "product_description": "Description 1"
            }
        ]
    },
    {
        "group_title": "Hot Selling",
        "group_type": 2,
        "products": [
            {
                "product_id": 12,
                "product_name": "Product 12",
                "product_price": "Rs 1,999/-",
                "product_category": "Instruments",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 13,
                "product_name": "Product 13",
                "product_price": "Rs 1,999/-",
                "product_category": "Instruments",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 14,
                "product_name": "Product 14",
                "product_price": "Rs 1,999/-",
                "product_category": "Instruments",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 15,
                "product_name": "Product 15",
                "product_price": "Rs 1,999/-",
                "product_category": "Instruments",
                "product_image": "",
                "product_description": "Description 1"
            },
            {
                "product_id": 16,
                "product_name": "Product 16",
                "product_price": "Rs 1,999/-",
                "product_category": "Instruments",
                "product_image": "",
                "product_description": "Description 1"
            }
        ]
    }

可以使用数组的过滤方法:

//this filter will return you all objects whose group type is equal to 1
let collectionArray = (product_groups?.filter({[=10=].group_type == 1})) 
//this filter will return you all objects whose group type is equal to 2
let tableArray = (product_groups?.filter({[=10=].group_type == 2}))

组标题为第 header 部分:

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      return tableArray[section].group_title
}

要在每个单元格中显示的产品:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      //cell code...
      cell.titleLabel.text = tableArray[indexPath.section][indexpPath.row].product_name
      // cell code
}