How to group tableview cells based on field in JSON array

前端 未结 2 1497
半阙折子戏
半阙折子戏 2021-01-28 03:36

Essentially I have am using JSON data to create an array and form a tableview.

I would like the table cells to be grouped by one of the fields from the JSON array.

2条回答
  •  有刺的猬
    2021-01-28 04:30

    You can achieve this by making an array of array. So something like this

    [[{"customer": "customer1", "number": "123"}, {"customer": "customer1", "number": "456"}], [{"customer": "customer2", "number": "678"}, {"customer": "customer2", "number": "890"}]]

    This is not the only data structure you can use to group. Another possibility is:

    {"customer1": [{"customer": "customer1", "number": "123"}, {"customer": "customer1", "number": "456"}], "customer2": [{"customer": "customer2", "number": "678"}, {"customer": "customer2", "number": "890"}]}

    Then you can use UITableView sections to group by customers. Section count would be the number of inside arrays and each section would contain as many rows as there are numbers in that inside array.

提交回复
热议问题