2016-03-06 81 views
0

我不知道我在這裏做最好的設計...計算嵌套在另一個結構中的結構數量?

我正在編寫一個應用程序,其中我有章節類別的嵌套章節。所有的值都將被硬編碼。例如:

struct Chapter1 { 
    struct Category1{ 
     let name = "#1" 
     let content = "Lorem Ipsum" 
    } 

    struct Category2{ 
     let name = "#2" 
     let content = "Lorem Ipsum Ipsum" 
    } 

    struct Category3{ 
     let name = "#3" 
     let content = "Ipsum Lorem Ipsum" 
    } 
} 

現在的問題是,我想返回一個numberOfSectionsInTableView類別的數量。我如何計算這些?有沒有辦法?或者,也許我的設計不是正確的?

然後,我需要通過一個segue傳遞結構體名稱...可能嗎?

目前,我發現的解決方案非常不雅觀。在Chapter1結構體中,我放置了一個包含「Category1」,「Category2」等的數組......這不是最優的!我也沒有找到一個解決方案來做到這一點:

var x = "Category1" 
var nameOfTheSelectedCategory = Chapter1.x.name 

甚至不知道這是否是可能的,但它可能是一個解決辦法。我也試圖與一個開關,但我有同樣的問題...

謝謝!

回答

3

你對什麼是類型和什麼是價值感到困惑。您已經定義了四種類型,但您需要的是兩種類型,以及這些類型的一些實例(值)。

這裏有您需要的類型:

struct Chapter { 
    let categories: [Category] 
} 

struct Category { 
    let name: String 
    let content: String 
} 

,這裏是包含Chapter類型,其中包含Category類型的三個值中的一個值的數組值:

let chapters: [Chapter] = [ 
    Chapter(categories: [ 
     Category(name: "Data Structures", content: "structs, classes, enums, tuples, etc."), 
     Category(name: "Algorithms", content: "sorting, searching, calculating, etc."), 
     Category(name: "Programs", content: "Flappy Bird, Microsoft Word, etc."), 
    ]) 
] 

您可以定義您的表格視圖數據源是這樣的:

class MyDataSource: NSObject, UITableViewDataSource { 

    let chapters: [Chapter] = [ 
     Chapter(categories: [ 
      Category(name: "Data Structures", content: "structs, classes, enums, tuples, etc."), 
      Category(name: "Algorithms", content: "sorting, searching, calculating, etc."), 
      Category(name: "Programs", content: "Flappy Bird, Microsoft Word, etc."), 
      ]) 
    ] 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return chapters.count 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return chapters[section].categories.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("CategoryCell", forIndexPath: indexPath) as! CategoryCell 
     let category = chapters[indexPath.section].categories[indexPath.row] 
     cell.category = category 
     return cell 
    } 

} 

如果segue連接出故事板中的單元格,則單元格本身就是發件人,因此您可以像這樣處理它:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "CategoryDetail" { 
      let cell = sender as! CategoryCell 
      let categoryDetailViewController = segue.destinationViewController as! CategoryDetailViewController 
      categoryDetailViewController.category = cell.category 
     } 
    } 
+0

不錯!讓我們走吧! – petaire

0

我想你應該考慮接近你的數據模型有點不同,看看做這樣的事情

class Chapter { 
    let name: String 
    var sections: [Category] 

    init(name: String, sections: [Category]){ 
     self.name = name 
     self.sections = sections 
    } 
} 
struct Category { 
    let name: String 
    let content: String 
} 

var newChapter = Chapter(name: "One", sections: [Category(name: "#1", content: "Lorem Ipsum")]) 

newChapter.sections.append(Category(name: "#2", content: "Test")) 

,如果你需要通過使self.data = [Chapter],然後以支持多個章節,您可以進一步擴展你的模型在numberOfSectionsInTableView返回self.data.countnumberOfRowsInSection返回self.data[section].sections.count

+0

謝謝!羅布更快(這就是她說的!) – petaire

+0

呃我6分鐘快了哈哈。 – sschale

+0

哦,你是對的!抱歉! – petaire