【问题标题】:Counting the number of struct nested in another struct?计算嵌套在另一个结构中的结构的数量?
【发布时间】:2016-03-06 17:39:18
【问题描述】:

我不确定我是否在这里做最好的设计……

我正在编写一个应用程序,其中包含嵌套章节的章节类别。所有值都将被硬编码。例如:

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 中返回 Category 的数量。我怎么数那些?有办法吗?或者也许我的设计不合适?

然后,我需要通过 segue 传递结构名称……这可能吗?

目前,我找到的解决方案非常不优雅。在第 1 章结构中,我放置了一个包含“Category1”、“Category2”等的数组……这不是最佳的!而且我没有找到解决方案:

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

甚至不知道这是否可能,但它可能是一个解决方案......我也尝试过使用开关,但我遇到了同样的问题......

谢谢!

【问题讨论】:

    标签: ios swift struct


    【解决方案1】:

    您对什么是类型和什么是值感到困惑。您已经定义了四种类型,但您需要的是两种类型,以及这些类型的一些实例(值)。

    以下是您需要的类型:

    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
            }
        }
    

    【讨论】:

    • 不错!就这样吧!
    【解决方案2】:

    我认为你应该考虑以不同的方式处理你的数据模型,看看做这样的事情

    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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-07
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多