问题
My json data is an array of object in which inner array of objects data are there till 3 levels. I took first level json data in sections and 2nd level data in cell. now how can I display the third level of data in single tableview ??
you can see the screen shot here
Till now only two levels done here. I need to display the 3rd label like this along with 2nd level..
my json data..
[
{
"id": "244",
"name": "PIZZAS",
"image": "",
"coupon": "1",
"icon": "",
"order": "1",
"aname": "",
"options": "2",
"subcategory": [
{
"id": "515",
"name": "MARGARITA",
"description": "Cheese and Tomato",
"image": "",
"icon": "",
"coupon": "1",
"order": "1",
"aname": "",
"options": "2",
"item": [
{
"id": "1749",
"name": "9 Inch Thin & Crispy Margarita",
"description": "",
"price": "3.40",
"coupon": "1",
"image": "",
"options": "2",
"order": "1",
"addon": "495",
"aname": "",
"icon": ""
},
回答1:
You could use UITableViewDelegate
s method tableView(_:indentationLevelForRowAt:)
to indent your 3rd level. Just implement that method in your delegate and return e.g. 20
for the rows that represent your 3rd level, 0
for all other rows.
UPDATE: Now I think to understand your problem. You will have to flatten your hierarchy because table views only support 2 levels natively. The 1st level can be implemented by means of tableview sections (as you already do). The 2nd and 3rd levels then can be implemented as rows, so you have to collapse these levels into one:
Pizzas Pizzas
Margaritha Margaritha
9InchThin 9InchThin
12InchThin --> 12InchThin
BBQBasic BBQBasic
9InchThin 9InchThin
12InchThin 12InchThin
In terms of Swift implementation, I'd suggest to use an Enum for representing the flattened rows:
enum ListItem {
case TitleItem(TitleData) // represents 2nd-level items
case DetailItem(DetailData) // represents 3rd-level items
}
(TitleData
is your model object containig e.g. "Margharita"/"Cheese and Tomato", DetailData
is the model containing e.g. "9 Inch Thin & Crispy Margharita"/"£3,40".)
In viewDidLoad:
create an array of ListItem
for every section in your table: add a TitleItem
for every title, followed by DetailItem
s for all nested details. From your tableView:numberOfRowsInSection:
return the number of list items in that section. And finally in tableView:cellForRowAtIndexPath:
return a cell depending on the type of the list item:
let listItem = listItemForIndexPath(indexPath)
switch listItem {
case .TitleItem(let titleData): return titleCellWithData(titleData)
case .DetailData(let detailData): return detailCellWithData(detailData)
}
... where titleCellWithData
and detailCellWithData
of course should dequeue reusable cells from the table view.
来源:https://stackoverflow.com/questions/38460986/how-to-display-3-level-of-data-into-one-single-tableview-in-swift