Swift - 使用 UITableView 向下钻取分层数据 [英] Swift - Drill down hierarchical data with UITableView

查看:30
本文介绍了Swift - 使用 UITableView 向下钻取分层数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个应用来导航数据层次结构.我一直在咨询这个 Drill-down Hierarchical UITableView 页面,但实际上并没有知道如何使用 xml 来实现这种节点结构来实现这一点.我试图避免创建 100 个 tableviewscontrollers 等.从我目前的理解来看,我相信我需要使用节点.在我一直向下钻取之后,我将需要使用不同的视图控制器,但我相信我知道该怎么做.

I want to build an app to navigate a data hierarchy. I have been consulting this Drill-down Hierarchical UITableView page and don't really know how to implement the sort of node structure using xml to implement this. I'm trying to avoid creating 100s of tableviewscontrollers, etc. From my understanding so far I believe I need to use nodes. After I have drilled all the way down then I will need to use a different viewcontroller but I believe I understand how todo that.

这是我的 XML 文件的一个小例子.如有必要,我可以对其进行更改以使内容正常工作.

Here is a mini example of my XML file. I can make changes to it if necessary to make stuff work.

<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 文件.我不知道如何打印样本.

I am using this code to parse my xml file. I'm not sure how to print a sample out.

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 示例中没有看到任何匹配的内容,但我假设它在那里).

It looks like the parser hopes to generate a hierarchy of categories. (I don't see anything matching in the xml sample, but I'll assume it's there).

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

First thing that's needed is a tree-structured Category object. At it's simplest, this is an NSObject subclass that has a name and an array of children which -- importantly -- is an array of Category.

Category 上的一个类方法可以进行 xml 解析.将xml解析成树状结构的基本大纲是:

A class method on Category can do the xml parsing. The basic outline of parsing xml into a tree structure is:

  • 在开始文档上,构建根.使其成为当前节点.
  • 在开始元素上,创建一个子元素.将它的父节点设置为当前节点.使子节点成为当前节点.
  • 在找到其他任何东西(如字符)时,在当前节点上设置属性.
  • 在结束元素上,使当前节点的父节点成为当前节点.
  • 在结束文档时,您已完成.

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

That tree is the tree that's being discussed in the other question. The whole idea is only possible if you have a tree structure and a notion of the current node (or current category) being browsed.

这篇关于Swift - 使用 UITableView 向下钻取分层数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆