2015-08-28 64 views
1

我能夠在UITableView中的數組字符串。當程序執行時。以下顯示:Swift:TableView,循環一個文本數組

Chp1 
Chp2 
chp3 

for循環是我ATM的工作..

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

使用的NSDictionary,商店章節名稱爲「關鍵」和部分陣列作爲「價值」 你會碰到這樣的:

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

根據...你應該填寫「書」字典。 http://www.appcoda.com/swift-programming-language-intro/ 請參閱「字典」一章。 – Igor