-1

i'm trying to achieve the following. i'm mapping an xml string into a tableview. This part is working fine, however i want to make sections in the tableview as well. I need the LINENAME to be the SectionName and the NODENAMES should be underneath those sections as rows. Below is an example of how it should look like.

  • header (LINE1)

    • Cell (NODE1)
    • Cell (NODE2)
  • header (LINE2)

    • Cell (Node1)

etc...

Could somebody give me some more information on how to achieve this?

Thanks in advance.

var LineRows: [LineRow] = []

override func viewDidLoad() {
    super.viewDidLoad()

    let xmlString = """
    <?xml version="1.0" encoding="utf-8"?>
    <RESULT>
        <ROWSET>
            <ROW>
                <LINENAME>LINE1</LINENAME>
                <NODENAME>NODE1</NODENAME>
                <LINEID>1</LINEID>
            </ROW>
            <ROW>
                <LINENAME>LINE1</LINENAME>
                <NODENAME>NODE2</NODENAME>
                <LINEID>1</LINEID>
            </ROW>
            <ROW>
                <LINENAME>LINE2</LINENAME>
                <NODENAME>NODE1</NODENAME>
                <LINEID>2</LINEID>
            </ROW>
        </ROWSET>
    </RESULT>
    """

    let data = Data(xmlString.utf8) // Data for deserialization (from XML to object)
    do {
        let xml = try XMLSerialization.xmlObject(with: data, options: [.default, .cdataAsString])
        let food = XMLMapper<LineResult>().map(XMLObject: xml)

        print(food?.Linerowset?.Linerows?.first?.Linename ?? "nil")

        self.LineRows = food!.Linerowset?.Linerows ?? []
        self.tableView.reloadData()
        
       // debugPrint(LineRows)

    } catch {
        print(error)
    }

}

Map XML String to Array of Objects

// MAP NODE DETAILS //


    class LineResult: XMLMappable {
        var nodeName: String!

        var error: String?
        var Linerowset: LineRowset?

        required init?(map: XMLMap) {}

        func mapping(map: XMLMap) {
            error <- map.attributes["error"]
            Linerowset <- map["ROWSET"]
        }
    }

    class LineRowset: XMLMappable {
        var nodeName: String!

        var Linerows: [LineRow]?

        required init?(map: XMLMap) {}

        func mapping(map: XMLMap) {
            Linerows <- map["ROW"]
        }
    }

    class LineRow: XMLMappable {
        var nodeName: String!

        var Linename: String?
        var Nodename: String?
        var LineLineID: String?

        required init?(map: XMLMap) {}

        func mapping(map: XMLMap) {
            Linename <- map["LINENAME"]
            Nodename <- map["NODENAME"]
            LineLineID <- map["LINEID"]

        }
    }

tableview:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return LineRows.count
        return LineRows.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let person = LineRows[indexPath.row]

        Cell.textLabel?.text = person.Nodename

        return Cell

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard
            let indexPath = tableView.indexPathForSelectedRow,
            let vc = segue.destination as? LineDetails
        else {
            return
        }

        let person = LineRows[indexPath.row]
        vc.lineID = person.LineLineID!
    }

debugprint(LineRows) response:

[ApiApp.LineController.LineRow, ApiApp.LineController.LineRow, ApiApp.LineController.LineRow]

dump(LineRows) response:

LINE1
▿ 3 elements
  ▿ ApiApp.LineController.LineRow #0
    ▿ nodeName: Optional("ROW")
      - some: "ROW"
    ▿ Linename: Optional("LINE1")
      - some: "LINE1"
    ▿ Nodename: Optional("NODE1")
      - some: "NODE1"
    ▿ LineLineID: Optional("1")
      - some: "1"
  ▿ ApiApp.LineController.LineRow #1
    ▿ nodeName: Optional("ROW")
      - some: "ROW"
    ▿ Linename: Optional("LINE1")
      - some: "LINE1"
    ▿ Nodename: Optional("NODE2")
      - some: "NODE2"
    ▿ LineLineID: Optional("1")
      - some: "1"
  ▿ ApiApp.LineController.LineRow #2
    ▿ nodeName: Optional("ROW")
      - some: "ROW"
    ▿ Linename: Optional("LINE2")
      - some: "LINE2"
    ▿ Nodename: Optional("NODE1")
      - some: "NODE1"
    ▿ LineLineID: Optional("2")
      - some: "2"
  • 2
    Not related, but don't name your var starting with an uppercase. `let Cell =` => `let cell =`, same for the properties of your struct/class. – Larme Jan 11 '21 at 18:00
  • Could you please uncomment `debugPrint(LineRows)` and add the output (i.e your dataSource structure) to the question? – Luca Sfragara Jan 11 '21 at 20:32
  • @LucaSfragara I have added the response from both Debugprint(LineRows) and Dump(LineRows). Thanks in advance! – patrickb100 Jan 11 '21 at 21:46
  • You need one section for each element of `LineRows`, correct? – Luca Sfragara Jan 12 '21 at 08:22
  • I'd regroup the rows. Instead of your current models directly put in the tableView VC, I'd use `struct LineSection { let lineId: String?; let lineName: String?; let nodes: [LineRow] }` and that would be easier, if you have later a `var sections: [LineSection];`: NumberOfSection: `sections.count`, NumberOfRowsInSection: `sections[section].nodes.count`,Element at IndexPath: `let person = sections[indexPath.section].nodes[indexPath.row]` – Larme Jan 12 '21 at 09:37
  • @LucaSfragara That's correct. Each section should be named to the Linename. Each section contains multiple nodenames – patrickb100 Jan 12 '21 at 11:18
  • @Larme Thank you for your reply, when i add "var sections: [LineSection];" to my class it gives an error "Class has no initializers" – patrickb100 Jan 12 '21 at 16:05
  • `var sections: [LineSection] = []`, and later `let sections = Dictionary(grouping: food!.Linerowset?.Linerows ?? [], by: { $0.lineId }).values.compactMap { Sections(lineId: $0.first?.lineId, lineName: $0.first?.lineName, nodes: $0) }.sorted(by: { $0.lineId ?? "" < $1.lineId ?? "" })` – Larme Jan 12 '21 at 16:13
  • @Larme Might it be possible for you to make a small example, i tried your solution but did not get it working – patrickb100 Jan 13 '21 at 17:32

1 Answers1

1

I'd make a ViewModel that will be a better fit for your table view.

struct LineSections {
    let lineId: String?
    let lineName: String?
    let nodes: [LineRow]
}

I guess that you have var LineRows: [LineRows] = [] somewhere, as a property of your ViewController. I'd change it with var lineSections: [LineSections] = [].

Then,

self.LineRows = food!.Linerowset?.Linerows ?? []
let rows = food?.Linerowset?.Linerows ?? []
self.lineSections = Dictionary(grouping: rows, by: { $0.lineId }).values.compactMap { LineSections(lineId: $0.first?.lineId, lineName: $0.first?.lineName, nodes: $0) }.sorted(by: { $0.lineId ?? "" < $1.lineId ?? "" }

In your table view populating:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lineSections[section].node.count
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let person = lineSections[indexPath.section].node[indexPath.row]
    cell.textLabel?.text = person.Nodename
    return cell
}

Not related : Name your var starting with a lowercase: let Cell => let cell etc.

Larme
  • 24,190
  • 6
  • 51
  • 81