为创建部分创建模型 - Swift
Posted
技术标签:
【中文标题】为创建部分创建模型 - Swift【英文标题】:Create a model for create section - Swift 【发布时间】:2019-10-02 10:54:42 【问题描述】:我需要创建一个带有部分的UITableView
,并且我的值如下所示:
let sectionTitles = ["section-1", "section-2", "section-3"]
let Items = [["id": 100, "name":"jim", "family": "rason"],["id": 200, "name":"jim22", "family": "rason22"], ["id": 300, "name":"jim33", "family": "rason33"]]
还有:
section-1
代表["id": 100, "name":"jim", "family": "rason"]
section-2
代表["id": 200, "name":"jim22", "family": "rason22"]
section-3
代表["id": 300, "name":"jim33", "family": "rason33"]
如何为它创建一个模型并填充这个模型?
有没有创建模型的在线网站?
【问题讨论】:
【参考方案1】:你需要
struct Section
let title:String
let content:[Content]
struct Content
let id:Int
let name,family:String
然后创建数组
let arr = [Section(title:"First",content:[Content(id:100,name:"na",family:"fam")])]
【讨论】:
【参考方案2】:您可以使用这样的类或结构创建模型
struct Section
let title:String
let content:SectionData
struct SectionData
let id:Int
let name,family:String
创建数组
var arr : [Section] = []
在数组中追加数据
arr.append(Section(title:”Section1”, content: Content(id: 1, name: "lokesh", family: "hdf")))
arr.append(Section(title:"Section2”, content: Content(id: 1, name: "lokesh", family: "hdf")))
arr.append(Section(title:"Section3”, content: Content(id: 1, name: "lokesh", family: "hdf")))
【讨论】:
以上是关于为创建部分创建模型 - Swift的主要内容,如果未能解决你的问题,请参考以下文章
swift / Core Data - 为数据模型创建“虚拟”NSManagedObject(无需托管对象即可测试事物)