为创建部分创建模型-Swift

时间:2019-10-02 10:54:42

标签: swift uitableview codable uitableviewsectionheader

我需要创建一个带有部分的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"]

如何为它创建模型并填充模型?

是否有用于创建模型的在线网站?

2 个答案:

答案 0 :(得分:2)

您需要

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")])]

答案 1 :(得分:0)

您可以使用Class或类似的结构创建模型

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")))