Как разобрать JSON в строки в строках внутри каждого раздела? - PullRequest
0 голосов
/ 13 января 2020

Я пытаюсь показать ТРЕТИЙ вложенный уровень в JSON данных. использование простого UITableView в swift 5 Моя проблема в том, как показать эти данные третьего уровня для каждого второго уровня (если они существуют) и как прояснить, что это третий раздел?

вот мой код:

struct mainCatagory : Decodable {
    let data: [Sections]
}

struct Sections: Decodable {
    let id: Int
    let name: String
    let subData: [getSubData]
}

struct getSubData: Decodable {
    let id: Int
    let name: String
    let Level03: [getSubLevels]?
}

struct getSubLevels: Decodable {
    let id: Int
    let name: String
}


class indexViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    var sections: [Sections] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        let urlString = "https://nabulsi.com/nabulsi_app/sections.json"
        if let url = URL(string: urlString){
            if let data = try? Data(contentsOf: url){
                // OK to parse
                parse(json: data)
            }
        }
    }

    func parse(json: Data) {
        let decoder = JSONDecoder()
        if let jsonNabSections = try? decoder.decode(mainCatagory.self, from: json){
              sections = jsonNabSections.data

                tableView.reloadData()
        }
    }

}


extension indexViewController: UITableViewDataSource, UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }




    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let MyLevel = sections[section].subData[section].Level03
        print(MyLevel?.count)

        let subDatas = sections[section].subData
        return subDatas.count ?? 0

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)


        let currentSection = sections[indexPath.section]
        let currentSubdata = currentSection.subData[indexPath.row]
        cell.textLabel?.text = currentSubdata.name
        return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
        view.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)

        let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width - 15, height: 40))
        lbl.font = UIFont.systemFont(ofSize: 20)
        lbl.text = sections[section].name
        lbl.textAlignment = .right
        lbl.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        view.addSubview(lbl)
        return view
    }



    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }


}

и вот файл данных JSON:

{
    "code": "200",
    "data": [
      {
             "name": "MAIN Level 01",
             "id": 1,
             "subData": [
                 {
                   "name": "SubLevel 01",
                   "id": 101,
                 },
                 {
                   "name": "SubLevel 02",
                   "id": 102,
                   "Level03": [
                     {
                       "name": "SubLevelTwo 01",
                       "id": 10201,
                     },
                     {
                       "name": "SubLevelTwo 02",
                       "id": 10202,
                     },
                     {
                       "name": "SubLevelTwo 03",
                       "id": 10203,
                     }
                   ],
                 },
                 {
                   "name": "SubLevel 03",
                   "id": 103,
                 },
                 {
                   "name": "SubLevel 04",
                   "id": 104,
                 },
                 {
                   "name": "SubLevel 05",
                   "id": 105,
                 }
             ],

         },


         {
                "name": "MAIN Level 02",
                "id": 2,
                "subData": [
                    {
                      "name": "SubLevel 01",
                      "id": 201,
                    },
                    {
                      "name": "SubLevel 02",
                      "id": 202,
                    },
                    {
                      "name": "SubLevel 03",
                      "id": 203,
                    },
                    {
                      "name": "SubLevel 04",
                      "id": 204,
                    }
                ],

            },

            {
                   "name": "MAIN Level 03",
                   "id": 3,
                   "subData": [
                       {
                         "name": "SubLevel 01",
                         "id": 301,
                       },
                       {
                         "name": "SubLevel 02",
                         "id": 302,
                       },
                       {
                         "name": "SubLevel 03",
                         "id": 303,
                       }
                   ],

               },

      ]
}

Пожалуйста, помогите мне с этим, я пытался дней :( Спасибо ..

...