从 Swift 中的 Json 响应中填充 TableSection
Posted
技术标签:
【中文标题】从 Swift 中的 Json 响应中填充 TableSection【英文标题】:Fill TableSection from Json Response in Swift 【发布时间】:2017-06-09 07:26:15 【问题描述】:如何使用此 JSON 响应填充 TableSection。
我已经实现了类似下拉菜单的设计,并在其中有一个原型单元格,其中并排有两个标签。 下面是我设计的结构
StudentDetailsSection V(按钮)
父亲的详细信息第 V 部分(按钮)
Mother'sDetailsSection V(按钮)
我还附上了我的设计截图
我的 JSON 响应在结构中。
"Student_Details":
[
"Student_ID": 1,
"School_ID": 1,
"FirstName": "Ammu",
"LastName": "Gopal",
"Date_Of_Birth": "2017-08-05T00:00:00",
"Gender": 1,
"Class": " LKG ",
"Section": "A",
"Mother_Tongue": "Tamil",
"Blood_Group": 1,
"Place_Of_Birth": "Chennai",
"Caste": 1,
"Religion": 1,
"Special_Person": 1,
"Languages_Known": "English,Tamil,Hindi",
"Distance_From_Residence_To_School": "11",
"Nationality": "Indian",
"Mode_Of_Transport": "School Transport",
"RTE": null,
"StudentStatus": 1,
"AppliedStatus": null,
"IsDeleted": null,
"Father_Name": "Kannan",
"Father_Mobile": "9845245254",
"Father_Email": "kannan@gmail.com",
"Father_Education": "Bsc",
"Father_Occupation": "Self-employer",
"Father_Name_Of_The_Organisation": "Bussiness",
"Father_Designation": "Self-Employer",
"Father_Annual_Income": "200000",
"Father_Office_Or_Business_Address": null,
"Mother_Name": "Mathi",
"Mother_Mobile": "9845245145",
"Mother_Email": "mathi@gmail.com",
"Mother_Education": "B com",
"Mother_Occupation": "Self Employer",
"Mother_Name_Of_The_Organisation": "Self Employer",
"Mother_Designation": "Self Employer",
"Mother_Annual_Income": "200000",
"Mother_Office_Or_Business_Address": null,
"Sibling_Details": null,
"Land_Line_Number": "044-2121444",
"Guardian_Name": null,
"Guardian_Mobile": null,
"Guardian_Email": null,
"Relationship": null,
"Guardian_Education": null,
"Guardian_Occupation": null,
"Guardian_Name_Of_The_Organisation": null,
"Guardian_Designation": null,
"Guardian_Annual_Income": null,
"Guardian_Office_Or_Business_Address": null,
"Guardian_Land_Line_Number": null,
"LeaveLetterApplied": null,
"Message": null,
"Date": null,
"Notify": null,
"Status": null
]
如何从此 jsonresponse 中获取数据并填写特定部分。希望得到帮助。提前致谢。
【问题讨论】:
【参考方案1】:假设您有 3 个静态部分:
1) 声明常量是一个好习惯。
let SEC_STUDENT = 0
let SEC_FATHER = 1
let SEC_MOTHER = 2
2) 从响应中获取数组
enum Student_Key : String
case ID = "Student_ID"
case FirstName = "FirstName"
case LastName = "LastName"
//Define all keys so on
enum Father_Key : String
case Name = "Father_Name"
case Mobile = "Father_Mobile"
//Define all keys so on
enum Mother_Key : String
case Name = "Mother_Name"
case Mobile = "Mother_Mobile"
//Define all keys so on
//Define global
var arrStudent : Array<[String:String]> = []
var arrMother : Array<[String:String]> = []
var arrFather : Array<[String:String]> = []
//Add new method to parse response
func parseData(response : [String:Any])
let arrayData = response["Student_Details"] as! [Any]
//You can load only one record in your current way.
let studentData = arrayData[0] as! [String:Any]
arrStudent.append(["key":Student_Key.ID.rawValue ,
"value" : String(describing: studentData[Student_Key.ID.rawValue])])
arrStudent.append(["key":Student_Key.FirstName.rawValue,
"value": String(describing: studentData[Student_Key.FirstName.rawValue])])
arrStudent.append(["key":Student_Key.LastName.rawValue,
"value": String(describing: studentData[Student_Key.LastName.rawValue])])
//You have to add all keys mannualy
arrMother.append(["key":Father_Key.Name.rawValue,
"value": String(describing: studentData[Father_Key.Name.rawValue])])
arrMother.append(["key":Father_Key.Mobile.rawValue,
"value": String(describing: studentData[Father_Key.Mobile.rawValue])])
arrFather.append(["key":Mother_Key.Name.rawValue,
"value": String(describing: studentData[Mother_Key.Name.rawValue])])
arrFather.append(["key":Mother_Key.Mobile.rawValue,
"value": String(describing: studentData[Mother_Key.Mobile.rawValue])])
yourTable.reloadData()
3) 返回部分,你已经实现了。
4) 返回行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if indexPath.section == SEC_STUDENT
return arrStudent.count
else if indexPath.Section == SEC_FATHER
return arrFather.count
else if indexPath.Section == SEC_MOTHER
return arrMother.count
else
return 0
5) 索引称重传感器
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier") as! YourCell
let dictData : [String:Any]
if indexPath.section == SEC_STUDENT
dictData = arrStudent[indexPath.row] as! [String:Any]
else if indexPath.Section == SEC_FATHER
dictData = arrFather[indexPath.row] as! [String:Any]
else if indexPath.Section == SEC_MOTHER
dictData = arrayData[indexPath.row] as! [String:Any]
cell.lblKey = data["key"]
cell.lblValue = data["value"]
return cell
** 备注
1) 这可能是您的要求,但这不是处理和显示 json 的正确方法。
2) 目前只能显示在学生记录上,应该是学生的详情页。
3) 我只是输入代码来展示实现目标的方法。你必须在你的代码中处理事情。
【讨论】:
非常感谢您的详细解答。我正在使用带有两个标签的自定义单元格,一个用于键,另一个用于值,并希望获取有关学生、父亲和母亲的所有详细信息。如何达到同样的效果?。请您更新您的答案吗?提前致谢。 是的,我有一个带有 2 个标签的自定义单元格。 然后你必须添加创建 3 个数组并且必须解析每个键。 你会用代码详细说明吗?否则您可以根据此更新您的答案,以便我可以接受它。先生,这就是我几天以来一直坚持的观点:(。提前致谢。 它的妈妈,是的,我可以更新答案。但是,如果您以这种方式想要它,它可能是不正确的。如果您只有一个学生记录,它将起作用。您不能以您试图维护的方式如此多的学生记录。以上是关于从 Swift 中的 Json 响应中填充 TableSection的主要内容,如果未能解决你的问题,请参考以下文章
如何在iOS swift的tableview中使用json响应中的键和值?
ios swift 2如何用json数据填充uipickerview [重复]