计算嵌套在另一个结构中的结构数量?
问题描述:
我不知道我在这里做最好的设计...计算嵌套在另一个结构中的结构数量?
我正在编写一个应用程序,其中我有章节类别的嵌套章节。所有的值都将被硬编码。例如:
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
甚至不知道这是否是可能的,但它可能是一个解决办法。我也试图与一个开关,但我有同样的问题...
谢谢!
答
你对什么是类型和什么是价值感到困惑。您已经定义了四种类型,但您需要的是两种类型,以及这些类型的一些实例(值)。
这里有您需要的类型:
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
}
}
答
我想你应该考虑接近你的数据模型有点不同,看看做这样的事情
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.count
和numberOfRowsInSection
返回self.data[section].sections.count
。
不错!让我们走吧! – petaire