gringoireDM / LNZTreeView

A swift TreeView
MIT License
235 stars 47 forks source link

How do I fill data According to our JSON Response. #36

Open kumarlav0 opened 4 years ago

kumarlav0 commented 4 years ago

How can I fill data According to our JSON Response?. How many Array I have to Create and How to Fill that data to them. My JSON Response is like: { "id": 2, "name": "Default Category", "featured_product": null, "custom_attributes": [{}] "children": [ { "id": 2.0, "name": "Default Category", "featured_product": null, "children": [{}] "custom_attributes": [{}] },{ "id": 2.1, "name": "Default Category", "featured_product": null, "children": [{}] "custom_attributes": [{}] } ] }

I want to Expand Cell according to "children": [{}] Array. If Children Array is Nil than I don't want to Expand and if it has some data Than it will Expand.

VaslD commented 4 years ago

You should first model your JSON response in Swift so that it's strongly typed and that it has everything you need to visualize the table. For example:

class Category {
    var id: String!
    var name: String!
    var subcategories: [Category]?
}

Then you'll need to pick any JSON library for Swift as the middle man converting Swift objects (models) to and from JSON texts.

When you finally get a bunch of Category objects treed in code, you can check their subcategories field to determine whether and how to show expandables. Basically you forward callbacks from your data source to the Category object; you can even implement the data source protocol on Category although it may not be best.

Also, I strongly suggest you work with JSON correctly. In JSON, nothing/empty is either implemented as null or []. [{}] means there is exactly one child but the child has no attributes (i.e. doesn't have "id" or "name"). That somewhat doesn't make sense in your data.