本文介绍了如何快速将3个级别的数据显示到一个表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的json数据是一个对象数组,其中对象数据的内部数组一直存在到3个级别.我在各节中获取了第一级json数据,在单元格中获取了第二级数据.现在如何在单个tableview中显示第三级数据?

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..

我的json数据.

[
{
"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": ""
      },

推荐答案

您可以使用UITableViewDelegate的方法tableView(_:indentationLevelForRowAt:)缩进第三级.只需在您的委托中实现该方法并返回例如20代表第三级,0代表其他所有行.

You could use UITableViewDelegates 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.

更新:现在,我想了解您的问题.您将不得不简化层次结构,因为表视图本机仅支持2个级别.可以通过tableview部分实现第1级(如您已经做的那样).然后可以将第二级和第三级实现为行,因此您必须将这些级别折叠为一个:

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

关于Swift的实现,我建议使用一个Enum来表示扁平化的行:

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是模型对象,例如"Margharita"/奶酪和番茄",DetailData是模型对象,例如"9英寸稀薄和脆皮的Margharita"/£3,40".)

(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".)

viewDidLoad:中,为表中的每个节创建一个ListItem数组:为每个标题添加TitleItem,然后为所有嵌套详细信息添加DetailItem.从您的tableView:numberOfRowsInSection:中返回该部分中列表项的数量.最后,在tableView:cellForRowAtIndexPath:中,根据列表项的类型返回一个单元格:

In viewDidLoad: create an array of ListItem for every section in your table: add a TitleItem for every title, followed by DetailItems 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)
}

...,其中titleCellWithDatadetailCellWithData当然应该从表视图中退出可重复使用的单元格.

... where titleCellWithData and detailCellWithData of course should dequeue reusable cells from the table view.

这篇关于如何快速将3个级别的数据显示到一个表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:12