populating table view with json data swift 4
I have got json data and I can parse the first part which is nav and name, however I need to parse the array children into a table view.
{
"nav": [
    {
        "name": "Home",
        "navigationName": "Home",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "CUSTOM",
            "target": "home",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": 
    },
    {
        "name": "New In",
        "navigationName": "New In",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "NO_LINK",
            "target": "",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": [
            "linkNewin"
        ],
        "children": [
            {
                "name": "New In Mens",
                "navigationName": "New In Mens",
                "icon": null,
                "navigation": {
                    "URI": "/men?facet:new=latest&sort=latest",
                    "type": "CATEGORY",
                    "target": "men",
                    "depth": null,
                    "data": null,
                    "filters": {
                        "facet:new": "latest",
                        "sort": "latest"
                    },
                    "urlStructure": {
                        "title": null,
                        "isFeatured": false,
                        "isCampaign": false
                    }
                },
                "styles": [
                    "linkNewin"
                ]
            },
That is the json data. I have parsed and populated the first name in the array nav, but cannot do it for the Children array.
I have created this data model so far:
struct Menu: Codable {
    var nav = [Menus]()
}
struct Menus: Codable {
    var name: String
    var children: ChildrensNames
}
struct ChildrensNames: Codable {
    var name: String
}
Does anyone have any ideas?
I have got the structs working so when I add a break point in I can see the children's names, however I do not know how to access these and add them to a second section in. Below is my table view set up
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
    return self.menu?.nav.count ?? 0
    } else if section == 1 {
        return self.menuItems?.children?.count ?? 0
    } else {
        return 2
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if cell == nil {
        cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
    }
    let navItem = self.menu?.nav[indexPath.row].name
    let childItem = self.menu?.nav[indexPath.row].children
    switch indexPath.section {
    case 0:
        cell?.textLabel?.text = navItem
        break
    case 1:
//      cell?.textLabel?.text =
        break
    default:
        break
}
    cell?.accessoryView = UIImageView(image: UIImage(named: "icons8-chevron-right-50"))
    return cell!
}
}
json swift uitableview
add a comment |
I have got json data and I can parse the first part which is nav and name, however I need to parse the array children into a table view.
{
"nav": [
    {
        "name": "Home",
        "navigationName": "Home",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "CUSTOM",
            "target": "home",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": 
    },
    {
        "name": "New In",
        "navigationName": "New In",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "NO_LINK",
            "target": "",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": [
            "linkNewin"
        ],
        "children": [
            {
                "name": "New In Mens",
                "navigationName": "New In Mens",
                "icon": null,
                "navigation": {
                    "URI": "/men?facet:new=latest&sort=latest",
                    "type": "CATEGORY",
                    "target": "men",
                    "depth": null,
                    "data": null,
                    "filters": {
                        "facet:new": "latest",
                        "sort": "latest"
                    },
                    "urlStructure": {
                        "title": null,
                        "isFeatured": false,
                        "isCampaign": false
                    }
                },
                "styles": [
                    "linkNewin"
                ]
            },
That is the json data. I have parsed and populated the first name in the array nav, but cannot do it for the Children array.
I have created this data model so far:
struct Menu: Codable {
    var nav = [Menus]()
}
struct Menus: Codable {
    var name: String
    var children: ChildrensNames
}
struct ChildrensNames: Codable {
    var name: String
}
Does anyone have any ideas?
I have got the structs working so when I add a break point in I can see the children's names, however I do not know how to access these and add them to a second section in. Below is my table view set up
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
    return self.menu?.nav.count ?? 0
    } else if section == 1 {
        return self.menuItems?.children?.count ?? 0
    } else {
        return 2
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if cell == nil {
        cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
    }
    let navItem = self.menu?.nav[indexPath.row].name
    let childItem = self.menu?.nav[indexPath.row].children
    switch indexPath.section {
    case 0:
        cell?.textLabel?.text = navItem
        break
    case 1:
//      cell?.textLabel?.text =
        break
    default:
        break
}
    cell?.accessoryView = UIImageView(image: UIImage(named: "icons8-chevron-right-50"))
    return cell!
}
}
json swift uitableview
 
 
 
 
 
 
 
 Possible duplicate of Parsing nested JSON using Codable
 
 – Adrian
 Nov 18 '18 at 3:16
 
 
 
add a comment |
I have got json data and I can parse the first part which is nav and name, however I need to parse the array children into a table view.
{
"nav": [
    {
        "name": "Home",
        "navigationName": "Home",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "CUSTOM",
            "target": "home",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": 
    },
    {
        "name": "New In",
        "navigationName": "New In",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "NO_LINK",
            "target": "",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": [
            "linkNewin"
        ],
        "children": [
            {
                "name": "New In Mens",
                "navigationName": "New In Mens",
                "icon": null,
                "navigation": {
                    "URI": "/men?facet:new=latest&sort=latest",
                    "type": "CATEGORY",
                    "target": "men",
                    "depth": null,
                    "data": null,
                    "filters": {
                        "facet:new": "latest",
                        "sort": "latest"
                    },
                    "urlStructure": {
                        "title": null,
                        "isFeatured": false,
                        "isCampaign": false
                    }
                },
                "styles": [
                    "linkNewin"
                ]
            },
That is the json data. I have parsed and populated the first name in the array nav, but cannot do it for the Children array.
I have created this data model so far:
struct Menu: Codable {
    var nav = [Menus]()
}
struct Menus: Codable {
    var name: String
    var children: ChildrensNames
}
struct ChildrensNames: Codable {
    var name: String
}
Does anyone have any ideas?
I have got the structs working so when I add a break point in I can see the children's names, however I do not know how to access these and add them to a second section in. Below is my table view set up
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
    return self.menu?.nav.count ?? 0
    } else if section == 1 {
        return self.menuItems?.children?.count ?? 0
    } else {
        return 2
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if cell == nil {
        cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
    }
    let navItem = self.menu?.nav[indexPath.row].name
    let childItem = self.menu?.nav[indexPath.row].children
    switch indexPath.section {
    case 0:
        cell?.textLabel?.text = navItem
        break
    case 1:
//      cell?.textLabel?.text =
        break
    default:
        break
}
    cell?.accessoryView = UIImageView(image: UIImage(named: "icons8-chevron-right-50"))
    return cell!
}
}
json swift uitableview
I have got json data and I can parse the first part which is nav and name, however I need to parse the array children into a table view.
{
"nav": [
    {
        "name": "Home",
        "navigationName": "Home",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "CUSTOM",
            "target": "home",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": 
    },
    {
        "name": "New In",
        "navigationName": "New In",
        "icon": null,
        "navigation": {
            "URI": null,
            "type": "NO_LINK",
            "target": "",
            "depth": null,
            "data": null,
            "filters": {},
            "urlStructure": {
                "title": null,
                "isFeatured": false,
                "isCampaign": false
            }
        },
        "styles": [
            "linkNewin"
        ],
        "children": [
            {
                "name": "New In Mens",
                "navigationName": "New In Mens",
                "icon": null,
                "navigation": {
                    "URI": "/men?facet:new=latest&sort=latest",
                    "type": "CATEGORY",
                    "target": "men",
                    "depth": null,
                    "data": null,
                    "filters": {
                        "facet:new": "latest",
                        "sort": "latest"
                    },
                    "urlStructure": {
                        "title": null,
                        "isFeatured": false,
                        "isCampaign": false
                    }
                },
                "styles": [
                    "linkNewin"
                ]
            },
That is the json data. I have parsed and populated the first name in the array nav, but cannot do it for the Children array.
I have created this data model so far:
struct Menu: Codable {
    var nav = [Menus]()
}
struct Menus: Codable {
    var name: String
    var children: ChildrensNames
}
struct ChildrensNames: Codable {
    var name: String
}
Does anyone have any ideas?
I have got the structs working so when I add a break point in I can see the children's names, however I do not know how to access these and add them to a second section in. Below is my table view set up
extension MenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0{
    return self.menu?.nav.count ?? 0
    } else if section == 1 {
        return self.menuItems?.children?.count ?? 0
    } else {
        return 2
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    if cell == nil {
        cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
    }
    let navItem = self.menu?.nav[indexPath.row].name
    let childItem = self.menu?.nav[indexPath.row].children
    switch indexPath.section {
    case 0:
        cell?.textLabel?.text = navItem
        break
    case 1:
//      cell?.textLabel?.text =
        break
    default:
        break
}
    cell?.accessoryView = UIImageView(image: UIImage(named: "icons8-chevron-right-50"))
    return cell!
}
}
json swift uitableview
json swift uitableview
edited Nov 18 '18 at 12:13


vadian
147k14159177
147k14159177
asked Nov 18 '18 at 1:39
Nathan TugwellNathan Tugwell
94
94
 
 
 
 
 
 
 
 Possible duplicate of Parsing nested JSON using Codable
 
 – Adrian
 Nov 18 '18 at 3:16
 
 
 
add a comment |
 
 
 
 
 
 
 
 Possible duplicate of Parsing nested JSON using Codable
 
 – Adrian
 Nov 18 '18 at 3:16
 
 
 
Possible duplicate of Parsing nested JSON using Codable
– Adrian
Nov 18 '18 at 3:16
Possible duplicate of Parsing nested JSON using Codable
– Adrian
Nov 18 '18 at 3:16
add a comment |
                                3 Answers
                            3
                        
active
oldest
votes
First of all let's rename Menus to avoid confusion. Let's name it NavigationItem.
The value for key children is also an array of NavigationItem and as some dictionaries don't have children make it optional.
If the structs are read-only adopt only Decodable and declare the struct members as constants.
struct Menu: Decodable {
    let nav : [NavigationItem] // Don't declare this struct member as empty array
}
struct NavigationItem : Decodable {
    let name : String
    let navigationName : String
    let children : [NavigationItem]?
}
 
 
 
 
 
 
 
 ok i have done that, how i would i then get the data from children and populate that in a table view?
 
 – Nathan Tugwell
 Nov 18 '18 at 10:47
 
 
 
 
 
 
 
 
 
 
 It depends on the data source structure of the table view. Basically use two for loops to iterate- navand- children
 
 – vadian
 Nov 18 '18 at 11:24
 
 
 
 
 
 
 
 
 
 
 let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
 
 – Nathan Tugwell
 Nov 18 '18 at 11:27
 
 
 
 
 
 
 
 
 
 
 
 
 that is what i have to populate the table at the moment, i can populate the nav name but not the child name
 
 – Nathan Tugwell
 Nov 18 '18 at 11:29
 
 
 
 
 
 
 
 
 
 
 Please edit your question, add the code and a description about the expected sections and rows of the table view.
 
 – vadian
 Nov 18 '18 at 11:43
 
 
 
|
show 3 more comments
In the JSON data “children” is an array,so you could try this:
struct Menus: Codable {
    var name: String
    var children: [ChildrensNames]  
}
Best Wishes!
add a comment |
Try this solution - in order to decode properly you need to define the data provider successfully :
struct Nav: Decodable {
 let nav: [NavItem]
}
struct NavItem: Decodable {
  var name : String?
  var navigationName : String?
  var children: [ChildrenArr]
  //And so on
}
then you can scode it like this:
do {
     let nav = try JSONDecoder().decode(Nav.self, from: data)
     //Do somthing
    } catch let error{
        print(error)
    }
so the tableview should look like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderCell = tableView.dequeueReusableCell(withIdentifier: "Section") as! SectionTableViewCell
    sectionHeaderCell.name.text = nav?.nav[section].name
    return sectionHeaderCell
}
func numberOfSections(in tableView: UITableView) -> Int {
    return self.nav?.nav.count ?? 0
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.nav?.nav[section]. children.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChildTableViewCell
    cell.name.text = self.nav?.nav[indexPath.section]. children[indexPath.row]
    return cell
}
the arr referees to the children array 
hope this will help
 
 
 
 
 
 
 
 Hi, Thanks for you answer, Do you have an idea about the rest of the question?
 
 – Nathan Tugwell
 Nov 18 '18 at 14:21
 
 
 
 
 
 
 
 
 
 
 Why do you need different sections?
 
 – ironRoei
 Nov 18 '18 at 14:24
 
 
 
 
 
 
 
 
 
 
 Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
 
 – Nathan Tugwell
 Nov 18 '18 at 14:27
 
 
 
 
 
 
 
 
 
 
 This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
 
 – ironRoei
 Nov 18 '18 at 14:31
 
 
 
 
 
 
 
 
 
 
 for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
 
 – Nathan Tugwell
 Nov 18 '18 at 14:44
 
 
 
|
show 9 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357177%2fpopulating-table-view-with-json-data-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
                                3 Answers
                            3
                        
active
oldest
votes
                                3 Answers
                            3
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all let's rename Menus to avoid confusion. Let's name it NavigationItem.
The value for key children is also an array of NavigationItem and as some dictionaries don't have children make it optional.
If the structs are read-only adopt only Decodable and declare the struct members as constants.
struct Menu: Decodable {
    let nav : [NavigationItem] // Don't declare this struct member as empty array
}
struct NavigationItem : Decodable {
    let name : String
    let navigationName : String
    let children : [NavigationItem]?
}
 
 
 
 
 
 
 
 ok i have done that, how i would i then get the data from children and populate that in a table view?
 
 – Nathan Tugwell
 Nov 18 '18 at 10:47
 
 
 
 
 
 
 
 
 
 
 It depends on the data source structure of the table view. Basically use two for loops to iterate- navand- children
 
 – vadian
 Nov 18 '18 at 11:24
 
 
 
 
 
 
 
 
 
 
 let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
 
 – Nathan Tugwell
 Nov 18 '18 at 11:27
 
 
 
 
 
 
 
 
 
 
 
 
 that is what i have to populate the table at the moment, i can populate the nav name but not the child name
 
 – Nathan Tugwell
 Nov 18 '18 at 11:29
 
 
 
 
 
 
 
 
 
 
 Please edit your question, add the code and a description about the expected sections and rows of the table view.
 
 – vadian
 Nov 18 '18 at 11:43
 
 
 
|
show 3 more comments
First of all let's rename Menus to avoid confusion. Let's name it NavigationItem.
The value for key children is also an array of NavigationItem and as some dictionaries don't have children make it optional.
If the structs are read-only adopt only Decodable and declare the struct members as constants.
struct Menu: Decodable {
    let nav : [NavigationItem] // Don't declare this struct member as empty array
}
struct NavigationItem : Decodable {
    let name : String
    let navigationName : String
    let children : [NavigationItem]?
}
 
 
 
 
 
 
 
 ok i have done that, how i would i then get the data from children and populate that in a table view?
 
 – Nathan Tugwell
 Nov 18 '18 at 10:47
 
 
 
 
 
 
 
 
 
 
 It depends on the data source structure of the table view. Basically use two for loops to iterate- navand- children
 
 – vadian
 Nov 18 '18 at 11:24
 
 
 
 
 
 
 
 
 
 
 let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
 
 – Nathan Tugwell
 Nov 18 '18 at 11:27
 
 
 
 
 
 
 
 
 
 
 
 
 that is what i have to populate the table at the moment, i can populate the nav name but not the child name
 
 – Nathan Tugwell
 Nov 18 '18 at 11:29
 
 
 
 
 
 
 
 
 
 
 Please edit your question, add the code and a description about the expected sections and rows of the table view.
 
 – vadian
 Nov 18 '18 at 11:43
 
 
 
|
show 3 more comments
First of all let's rename Menus to avoid confusion. Let's name it NavigationItem.
The value for key children is also an array of NavigationItem and as some dictionaries don't have children make it optional.
If the structs are read-only adopt only Decodable and declare the struct members as constants.
struct Menu: Decodable {
    let nav : [NavigationItem] // Don't declare this struct member as empty array
}
struct NavigationItem : Decodable {
    let name : String
    let navigationName : String
    let children : [NavigationItem]?
}
First of all let's rename Menus to avoid confusion. Let's name it NavigationItem.
The value for key children is also an array of NavigationItem and as some dictionaries don't have children make it optional.
If the structs are read-only adopt only Decodable and declare the struct members as constants.
struct Menu: Decodable {
    let nav : [NavigationItem] // Don't declare this struct member as empty array
}
struct NavigationItem : Decodable {
    let name : String
    let navigationName : String
    let children : [NavigationItem]?
}
answered Nov 18 '18 at 7:46


vadianvadian
147k14159177
147k14159177
 
 
 
 
 
 
 
 ok i have done that, how i would i then get the data from children and populate that in a table view?
 
 – Nathan Tugwell
 Nov 18 '18 at 10:47
 
 
 
 
 
 
 
 
 
 
 It depends on the data source structure of the table view. Basically use two for loops to iterate- navand- children
 
 – vadian
 Nov 18 '18 at 11:24
 
 
 
 
 
 
 
 
 
 
 let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
 
 – Nathan Tugwell
 Nov 18 '18 at 11:27
 
 
 
 
 
 
 
 
 
 
 
 
 that is what i have to populate the table at the moment, i can populate the nav name but not the child name
 
 – Nathan Tugwell
 Nov 18 '18 at 11:29
 
 
 
 
 
 
 
 
 
 
 Please edit your question, add the code and a description about the expected sections and rows of the table view.
 
 – vadian
 Nov 18 '18 at 11:43
 
 
 
|
show 3 more comments
 
 
 
 
 
 
 
 ok i have done that, how i would i then get the data from children and populate that in a table view?
 
 – Nathan Tugwell
 Nov 18 '18 at 10:47
 
 
 
 
 
 
 
 
 
 
 It depends on the data source structure of the table view. Basically use two for loops to iterate- navand- children
 
 – vadian
 Nov 18 '18 at 11:24
 
 
 
 
 
 
 
 
 
 
 let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
 
 – Nathan Tugwell
 Nov 18 '18 at 11:27
 
 
 
 
 
 
 
 
 
 
 
 
 that is what i have to populate the table at the moment, i can populate the nav name but not the child name
 
 – Nathan Tugwell
 Nov 18 '18 at 11:29
 
 
 
 
 
 
 
 
 
 
 Please edit your question, add the code and a description about the expected sections and rows of the table view.
 
 – vadian
 Nov 18 '18 at 11:43
 
 
 
ok i have done that, how i would i then get the data from children and populate that in a table view?
– Nathan Tugwell
Nov 18 '18 at 10:47
ok i have done that, how i would i then get the data from children and populate that in a table view?
– Nathan Tugwell
Nov 18 '18 at 10:47
It depends on the data source structure of the table view. Basically use two for loops to iterate
nav and children– vadian
Nov 18 '18 at 11:24
It depends on the data source structure of the table view. Basically use two for loops to iterate
nav and children– vadian
Nov 18 '18 at 11:24
let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
– Nathan Tugwell
Nov 18 '18 at 11:27
let navItem = self.menu?.nav[indexPath.row].name switch indexPath.section { case 0: cell?.textLabel?.text = navItem break case 1: for _ in navItem! { if self.menu?.nav[indexPath.row].children == nil { return cell! } else { for child in childItems! { cell?.textLabel?.text = }
– Nathan Tugwell
Nov 18 '18 at 11:27
that is what i have to populate the table at the moment, i can populate the nav name but not the child name
– Nathan Tugwell
Nov 18 '18 at 11:29
that is what i have to populate the table at the moment, i can populate the nav name but not the child name
– Nathan Tugwell
Nov 18 '18 at 11:29
Please edit your question, add the code and a description about the expected sections and rows of the table view.
– vadian
Nov 18 '18 at 11:43
Please edit your question, add the code and a description about the expected sections and rows of the table view.
– vadian
Nov 18 '18 at 11:43
|
show 3 more comments
In the JSON data “children” is an array,so you could try this:
struct Menus: Codable {
    var name: String
    var children: [ChildrensNames]  
}
Best Wishes!
add a comment |
In the JSON data “children” is an array,so you could try this:
struct Menus: Codable {
    var name: String
    var children: [ChildrensNames]  
}
Best Wishes!
add a comment |
In the JSON data “children” is an array,so you could try this:
struct Menus: Codable {
    var name: String
    var children: [ChildrensNames]  
}
Best Wishes!
In the JSON data “children” is an array,so you could try this:
struct Menus: Codable {
    var name: String
    var children: [ChildrensNames]  
}
Best Wishes!
edited Nov 18 '18 at 7:49
Kurucu
787823
787823
answered Nov 18 '18 at 7:41


GorCatGorCat
14
14
add a comment |
add a comment |
Try this solution - in order to decode properly you need to define the data provider successfully :
struct Nav: Decodable {
 let nav: [NavItem]
}
struct NavItem: Decodable {
  var name : String?
  var navigationName : String?
  var children: [ChildrenArr]
  //And so on
}
then you can scode it like this:
do {
     let nav = try JSONDecoder().decode(Nav.self, from: data)
     //Do somthing
    } catch let error{
        print(error)
    }
so the tableview should look like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderCell = tableView.dequeueReusableCell(withIdentifier: "Section") as! SectionTableViewCell
    sectionHeaderCell.name.text = nav?.nav[section].name
    return sectionHeaderCell
}
func numberOfSections(in tableView: UITableView) -> Int {
    return self.nav?.nav.count ?? 0
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.nav?.nav[section]. children.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChildTableViewCell
    cell.name.text = self.nav?.nav[indexPath.section]. children[indexPath.row]
    return cell
}
the arr referees to the children array 
hope this will help
 
 
 
 
 
 
 
 Hi, Thanks for you answer, Do you have an idea about the rest of the question?
 
 – Nathan Tugwell
 Nov 18 '18 at 14:21
 
 
 
 
 
 
 
 
 
 
 Why do you need different sections?
 
 – ironRoei
 Nov 18 '18 at 14:24
 
 
 
 
 
 
 
 
 
 
 Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
 
 – Nathan Tugwell
 Nov 18 '18 at 14:27
 
 
 
 
 
 
 
 
 
 
 This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
 
 – ironRoei
 Nov 18 '18 at 14:31
 
 
 
 
 
 
 
 
 
 
 for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
 
 – Nathan Tugwell
 Nov 18 '18 at 14:44
 
 
 
|
show 9 more comments
Try this solution - in order to decode properly you need to define the data provider successfully :
struct Nav: Decodable {
 let nav: [NavItem]
}
struct NavItem: Decodable {
  var name : String?
  var navigationName : String?
  var children: [ChildrenArr]
  //And so on
}
then you can scode it like this:
do {
     let nav = try JSONDecoder().decode(Nav.self, from: data)
     //Do somthing
    } catch let error{
        print(error)
    }
so the tableview should look like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderCell = tableView.dequeueReusableCell(withIdentifier: "Section") as! SectionTableViewCell
    sectionHeaderCell.name.text = nav?.nav[section].name
    return sectionHeaderCell
}
func numberOfSections(in tableView: UITableView) -> Int {
    return self.nav?.nav.count ?? 0
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.nav?.nav[section]. children.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChildTableViewCell
    cell.name.text = self.nav?.nav[indexPath.section]. children[indexPath.row]
    return cell
}
the arr referees to the children array 
hope this will help
 
 
 
 
 
 
 
 Hi, Thanks for you answer, Do you have an idea about the rest of the question?
 
 – Nathan Tugwell
 Nov 18 '18 at 14:21
 
 
 
 
 
 
 
 
 
 
 Why do you need different sections?
 
 – ironRoei
 Nov 18 '18 at 14:24
 
 
 
 
 
 
 
 
 
 
 Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
 
 – Nathan Tugwell
 Nov 18 '18 at 14:27
 
 
 
 
 
 
 
 
 
 
 This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
 
 – ironRoei
 Nov 18 '18 at 14:31
 
 
 
 
 
 
 
 
 
 
 for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
 
 – Nathan Tugwell
 Nov 18 '18 at 14:44
 
 
 
|
show 9 more comments
Try this solution - in order to decode properly you need to define the data provider successfully :
struct Nav: Decodable {
 let nav: [NavItem]
}
struct NavItem: Decodable {
  var name : String?
  var navigationName : String?
  var children: [ChildrenArr]
  //And so on
}
then you can scode it like this:
do {
     let nav = try JSONDecoder().decode(Nav.self, from: data)
     //Do somthing
    } catch let error{
        print(error)
    }
so the tableview should look like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderCell = tableView.dequeueReusableCell(withIdentifier: "Section") as! SectionTableViewCell
    sectionHeaderCell.name.text = nav?.nav[section].name
    return sectionHeaderCell
}
func numberOfSections(in tableView: UITableView) -> Int {
    return self.nav?.nav.count ?? 0
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.nav?.nav[section]. children.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChildTableViewCell
    cell.name.text = self.nav?.nav[indexPath.section]. children[indexPath.row]
    return cell
}
the arr referees to the children array 
hope this will help
Try this solution - in order to decode properly you need to define the data provider successfully :
struct Nav: Decodable {
 let nav: [NavItem]
}
struct NavItem: Decodable {
  var name : String?
  var navigationName : String?
  var children: [ChildrenArr]
  //And so on
}
then you can scode it like this:
do {
     let nav = try JSONDecoder().decode(Nav.self, from: data)
     //Do somthing
    } catch let error{
        print(error)
    }
so the tableview should look like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let sectionHeaderCell = tableView.dequeueReusableCell(withIdentifier: "Section") as! SectionTableViewCell
    sectionHeaderCell.name.text = nav?.nav[section].name
    return sectionHeaderCell
}
func numberOfSections(in tableView: UITableView) -> Int {
    return self.nav?.nav.count ?? 0
}
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.nav?.nav[section]. children.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ChildTableViewCell
    cell.name.text = self.nav?.nav[indexPath.section]. children[indexPath.row]
    return cell
}
the arr referees to the children array 
hope this will help
edited Nov 19 '18 at 7:53
answered Nov 18 '18 at 14:17
ironRoeiironRoei
342112
342112
 
 
 
 
 
 
 
 Hi, Thanks for you answer, Do you have an idea about the rest of the question?
 
 – Nathan Tugwell
 Nov 18 '18 at 14:21
 
 
 
 
 
 
 
 
 
 
 Why do you need different sections?
 
 – ironRoei
 Nov 18 '18 at 14:24
 
 
 
 
 
 
 
 
 
 
 Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
 
 – Nathan Tugwell
 Nov 18 '18 at 14:27
 
 
 
 
 
 
 
 
 
 
 This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
 
 – ironRoei
 Nov 18 '18 at 14:31
 
 
 
 
 
 
 
 
 
 
 for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
 
 – Nathan Tugwell
 Nov 18 '18 at 14:44
 
 
 
|
show 9 more comments
 
 
 
 
 
 
 
 Hi, Thanks for you answer, Do you have an idea about the rest of the question?
 
 – Nathan Tugwell
 Nov 18 '18 at 14:21
 
 
 
 
 
 
 
 
 
 
 Why do you need different sections?
 
 – ironRoei
 Nov 18 '18 at 14:24
 
 
 
 
 
 
 
 
 
 
 Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
 
 – Nathan Tugwell
 Nov 18 '18 at 14:27
 
 
 
 
 
 
 
 
 
 
 This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
 
 – ironRoei
 Nov 18 '18 at 14:31
 
 
 
 
 
 
 
 
 
 
 for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
 
 – Nathan Tugwell
 Nov 18 '18 at 14:44
 
 
 
Hi, Thanks for you answer, Do you have an idea about the rest of the question?
– Nathan Tugwell
Nov 18 '18 at 14:21
Hi, Thanks for you answer, Do you have an idea about the rest of the question?
– Nathan Tugwell
Nov 18 '18 at 14:21
Why do you need different sections?
– ironRoei
Nov 18 '18 at 14:24
Why do you need different sections?
– ironRoei
Nov 18 '18 at 14:24
Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
– Nathan Tugwell
Nov 18 '18 at 14:27
Because I want the children array to display in a different section, so when I can display them I’m going to make it into a expandable table view
– Nathan Tugwell
Nov 18 '18 at 14:27
This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
– ironRoei
Nov 18 '18 at 14:31
This is children for you?: { "name": "Home", "navigationName": "Home", "icon": null, "navigation": { "URI": null, "type": "CUSTOM", "target": "home", "depth": null, "data": null, "filters": {}, "urlStructure": { "title": null, "isFeatured": false, "isCampaign": false } }, "styles": }
– ironRoei
Nov 18 '18 at 14:31
for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
– Nathan Tugwell
Nov 18 '18 at 14:44
for me children is: `"children": [ { "name": "New In Mens", "navigationName": "New In Mens", "icon": null, "navigation": { "URI": "/men?facet:new=latest&sort=latest", "type": "CATEGORY", "target": "men", "depth": null, ]
– Nathan Tugwell
Nov 18 '18 at 14:44
|
show 9 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53357177%2fpopulating-table-view-with-json-data-swift-4%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Parsing nested JSON using Codable
– Adrian
Nov 18 '18 at 3:16