2015-08-28 67 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