使用同一数组

时间:2017-10-25 05:39:26

标签: ios swift uicollectionview tableview xcode9

我想展示" group_type"的产品在CollectionView和" group_type"的产品中== 1;在TableView中== 2。

我想按顺序填充TableView" group_title"将是Section Header和Products in Array" 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"
            }
        ]
    }

1 个答案:

答案 0 :(得分:1)

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

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

将小组标题作为小节标题:

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