Swift-使用UITableView向下搜索分层数据



我想构建一个应用程序来导航数据层次结构。我一直在查阅这个向下搜索层次UITableView页面,不知道如何使用xml实现这种节点结构。我试图避免创建100个表视图控制器等。根据我目前的理解,我认为我需要使用节点。在我一路往下钻之后,我需要使用不同的视图控制器,但我相信我知道如何做到这一点。

下面是我的XML文件的一个小示例。如果有必要的话,我可以对它进行修改,使它发挥作用。

<hnt>
<face>
<action id="1">
<name>Occipitofrontalis</name>
<type>data</type>
<description>data</description>
</action>
</face>
<temporoman>
<mandibulardep>
<action id="1">
<name>Occipitofrontalis</name>
<type>data</type>
<description>data</description>
</action>
<action id="2">
<name>Occipitofrontalis</name>
<type>data</type>
<description>data</description>
</action>
</mandibulardep>
</temporoman>
</hnt>
<face>
</face>

我正在使用这段代码来解析我的xml文件。我不知道如何打印样品。

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()
}
}
}
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
}
}
}

看起来解析器希望生成一个类别层次结构。(我在xml示例中没有看到任何匹配的内容,但我认为它在那里)。

首先需要的是一个树结构的Category对象。最简单的是,这是一个NSObject子类,它有一个name和一个子数组,重要的是,它是Category的数组。

Category上的类方法可以进行xml解析。将xml解析为树结构的基本轮廓是:

  • 在启动文档时,构建根。使其成为当前节点
  • 在start元素上,创建一个子元素。将其父节点设置为当前节点。使子节点成为当前节点
  • 在查找其他内容(如字符)时,设置当前节点的属性
  • 在end元素上,使当前节点的父节点成为当前节点
  • 在最终文档中,您完成了

这棵树就是另一个问题中正在讨论的树。只有当您有一个树结构和正在浏览的当前节点(或当前类别)的概念时,整个想法才有可能实现。

最新更新