Populate CollectionView and TableView using object

2019-10-04 13:10发布

问题:

I want to show Products of "group_type" == 1 in CollectionView and Products of "group_type" == 2 in TableView.

I want to populate TableView in order where "group_title" will be Section Header and Products in Array "Products" as Rows. Below is my JSON.

What Swift code do I need?

  "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:

You can use filter method of array:

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

For group title to be section header:

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

For products to display in each cell:

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