1 Reply
      Latest reply: Feb 15, 2017 2:23 AM by eskimo RSS
      RedNumberOnion Level 1 Level 1 (0 points)

        Hello,

         

        I'm attempting to create a drill-down UITableView with xml data and believe I need to use linked-lists, etc but have no clue where to start/what I'm doing. I'm currently doing something like this for each page but just realized that it was totally the wrong way to go about doing it.

         

        
        import UIKit
        class MasterTableViewController: UITableViewController, XMLParserDelegate {
           
            var mainCat: [MainCategories] = []
            var eName: String = String()
            var mainCategoriesTitle = String()
           
            override func viewDidLoad() {
                super.viewDidLoad()
               
                if let path = Bundle.main.url(forResource: "01 - MainCategories", withExtension: "xml") {
                    if let parser = XMLParser(contentsOf: path) {
                        parser.delegate = self
                        parser.parse()
                    }
                }
            }
           
            override func didReceiveMemoryWarning() {
                super.didReceiveMemoryWarning()
                /
            }
           
            /
           
            override func numberOfSections(in tableView: UITableView) -> Int {
                return 1
            }
           
            override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                return mainCat.count
            }
           
            override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
               
                let mCaty = mainCat[(indexPath as NSIndexPath).row]
               
                cell.textLabel?.text = mCaty.mainCategoriesTitle
               
                return cell
            }
           
            override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                if segue.identifier == "LevelTwo" {
                    if let indexPath = self.tableView.indexPathForSelectedRow {
                       
                        let controller = segue.destination as! LevelTwoTableViewController
                       
                        let mCaty = mainCat[(indexPath as NSIndexPath).row]
                        controller.selectedName = mCaty.mainCategoriesTitle
        /
                    }
                }
            }
           
            func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
                eName = elementName
                if elementName == "mainCat" {
                    mainCategoriesTitle = String()
                }
            }
           
           
            func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
                if elementName == "mainCat" {
                   
                    let mCat = MainCategories()
                    mCat.mainCategoriesTitle = mainCategoriesTitle
                   
                    mainCat.append(mCat)
                }
            }
           
           
            func parser(_ parser: XMLParser, foundCharacters string: String) {
                let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
               
                if (!data.isEmpty) {
                    if eName == "title" {
                        mainCategoriesTitle += data
                    }
                }
            }
        }
        
        • Re: Drill down hierarchical data with UITableView
          eskimo Apple Staff Apple Staff (7,960 points)

          Do you want to work with arbitrary XML?  Or with XML that represents some specific type of data?  What does that data look like?

          Regardless, I recommend that you break this problem up into two parts:

          • Parsing the XML into a tree of model objects

          • Displaying that tree with a nav controller-based drill down UI

          This separation will make the code much easier to write.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"