简体   繁体   中英

Swift: TableView, looping an array of text

I am able to string the array in a UITableView. when the program is executed. The following Displays:

Chp1
Chp2 
chp3

this for loop is what I have working atm..

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

                    }

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

it does the following navigations..

    Chp1 > Sec1
    Chp2 > Sec2
    Chp3 > Sec3

How can I go about getting it to do the following instead?

    Chp1 > Sec1
         > Sec2
         > Sec3

This is the whole snippet i've got going:

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]])]
                    }....

Use NSDictionary, store chapter name as "Key" and Sections Array as "Value" You will have something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM