Swift:TableView,循环一个文本数组

时间:2015-08-28 13:02:26

标签: ios arrays swift for-loop tableview

我能够在UITableView中将数组串起来。程序执行时。以下显示:

Chp1
Chp2 
chp3

这个for循环是我工作的东西..

    for i in 1 ..< Chapters.count {
                        FirstTableArray += [(Chapters[i])]

                    }

                    for i in 1 ..< Sections.count {
                        SecondTableArray += [SecondTable(SecondTitle:[Sections[i]])]
                    }

它执行以下导航..

    Chp1 > Sec1
    Chp2 > Sec2
    Chp3 > Sec3

我怎样才能让它做以下事情呢?

    Chp1 > Sec1
         > Sec2
         > Sec3

这是我要完成的整个片段:

if var path = NSBundle.mainBundle().pathForResource("Chapters", ofType: "txt"){
                var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)
                if var content = (data){
                    var line: [String] = content.componentsSeparatedByString("\n")


                    let Chapters: [String] = content.componentsSeparatedByString("#")
                    let Sections: [String] = content.componentsSeparatedByString("1.")
                    let Headings: [String] = content.componentsSeparatedByString("/H")

                    for i in 1 ..< Chapters.count {
                        FirstTableArray += [(Chapters[i])]

                    }

                    for i in 1 ..< Sections.count {
                        SecondTableArray += [SecondTable(SecondTitle:[Sections[i]])]
                    }....

1 个答案:

答案 0 :(得分:1)

使用NSDictionary,将章节名称存储为&#34; Key&#34;和Sections Array as&#34; Value&#34; 你会有类似的东西:

NSDictionary *book;
...
chapters = book.allKeys; // array of chapters
sectionsForChapters = book[chapters[0]]; // array of sections for the first chapter