0

I have parsed a JSON file and now I am trying to create a table view with a section. The section is a dictionary inside the data model.

import Foundation

struct ActionResult: Codable {
    let data: [ActionElement]
}

struct ActionElement: Codable {
    let actionID: Int
    let actionItem: String
    let actionGoal: ActionGoal
    let actionImage: String
    let actionBenefit: ActionBenefit
    let actionSavings: Int
    let actionType: ActionType
    let actionDescription, actionTips: String
    let actionInformationURL: String
    let actionSponsorURL: String

    enum CodingKeys: String, CodingKey {
        case actionID = "ActionID"
        case actionItem = "ActionItem"
        case actionGoal = "ActionGoal"
        case actionImage = "ActionImage"
        case actionBenefit = "ActionBenefit"
        case actionSavings = "ActionSavings"
        case actionType = "ActionType"
        case actionDescription = "ActionDescription"
        case actionTips = "ActionTips"
        case actionInformationURL = "ActionInformationURL"
        case actionSponsorURL = "ActionSponsorURL"
    }
}

enum ActionBenefit: String, Codable {
    case costs = "Costs"
    case education = "Education"
    case environment = "Environment"
    case health = "Health"
}

enum ActionGoal: String, Codable {
    case cleanEnergy = "Clean Energy"
    case cleanWater = "Clean Water"
    case climateAction = "Climate Action"
    case economicGrowth = "Economic Growth"
    case goodHealth = "Good Health"
    case noPoverty = "No Poverty"
    case promoteEquality = "Promote Equality"
    case qualityEducation = "Quality Education"
    case responsibleConsumption = "Responsible Consumption"
    case zeroHunger = "Zero Hunger"
}

enum ActionType: String, Codable {
    case sticky = "Sticky"
    case todoe = "Todoe"
}

typealias Action = [ActionElement]

Now I am trying to create my table view section but I am getting the following error: Value of type '[ActionElement]' has no member 'actionGoal'

Two questions: How do I fix this? What is a good resource to understand more about data models and table views?

This is the code I am writing:

    var result: ActionResult?
    var index = 0
    
       
    override func viewDidLoad() {
        super.viewDidLoad()
        parseJSON()
        
        
    }
    
    // Table View Sections
    
    
    override func numberOfSections(in tableView: UITableView) -> Int {
            return result?.data.count ?? 0
            
          }

        override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return result?.data.actionGoal
       }
3
  • If the array [ActionElement] is your sections then what are the rows for each section? Commented Apr 11, 2022 at 21:13
  • the actionItem which is an element of ActionElement. I believe i am making a mistake at the 'cellForRowAt'. I am declaring let action = result?.data[indexPath.section].ActionElement[indexPath.row or actionItem[indexPath.row], but both are giving me an errors. In the table view it shows me the right sections, but it is showing me the same row for all sections, although the detail is different.
    – Nick Wols
    Commented Apr 11, 2022 at 21:49
  • As I see it you don’t have a natural section/rows design in your data structure so you have to take an extra step to change how your data is structured. Or skip using sections for now. Commented Apr 12, 2022 at 6:18

2 Answers 2

0

I believe you need to index the data and then use the raw value (string) as ActionGoal is an enum.

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return result?.data[section].actionGoal.rawValue
}
1
  • Thank you, that solved a couple of errors. I only see one section now. Do I need to change this code to see them all? '''override func numberOfSections(in tableView: UITableView) -> Int { return result?.data.count ?? 0'''
    – Nick Wols
    Commented Apr 11, 2022 at 21:23
0

You forgot to access the array element

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return result?.data[section].actionGoal.rawValue
   }
2
  • I have tried that too but then the following error appears: Type of expression is ambiguous without more context
    – Nick Wols
    Commented Apr 11, 2022 at 20:17
  • yep missed the fact of the enum - edited... I really recommend you to learn about how to work with table and collection views - generally speaking RayWenderlich has great books to learn swift - raywenderlich.com/books/ios-apprentice/v8.3/chapters/… Commented Apr 12, 2022 at 8:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.