我正在努力实现以下目标。我正在将一个 xml 字符串映射到一个 tableview 中。这部分工作正常,但是我也想在 tableview 中制作部分。我需要 LINENAME 作为 SectionName 并且 nodenameS 应该作为行位于这些部分的下方。下面是它应该是什么样子的示例。
-
标题 (LINE1)
- 单元格 (NODE1)
- 单元格 (NODE2)
-
标题 (LINE2)
- 单元(节点 1)
等等...
有人能给我一些关于如何实现这一目标的更多信息吗?
提前致谢。
var LineRows: [LineRow] = []
override func viewDidLoad() {
super.viewDidLoad()
let xmlString = """
<?xml version="1.0" encoding="utf-8"?>
<RESULT>
<rowset>
<ROW>
<LINENAME>LINE1</LINENAME>
<nodename>NODE1</nodename>
<LINEID>1</LINEID>
</ROW>
<ROW>
<LINENAME>LINE1</LINENAME>
<nodename>NODE2</nodename>
<LINEID>1</LINEID>
</ROW>
<ROW>
<LINENAME>LINE2</LINENAME>
<nodename>NODE1</nodename>
<LINEID>2</LINEID>
</ROW>
</rowset>
</RESULT>
"""
let data = Data(xmlString.utf8) // Data for deserialization (from XML to object)
do {
let xml = try XMLSerialization.xmlObject(with: data,options: [.default,.cdataAsString])
let food = XMLMapper<LineResult>().map(XMLObject: xml)
print(food?.Linerowset?.Linerows?.first?.Linename ?? "nil")
self.LineRows = food!.Linerowset?.Linerows ?? []
self.tableView.reloadData()
// debugPrint(LineRows)
} catch {
print(error)
}
}
将 XML 字符串映射到对象数组
// MAP NODE DetaILS //
class LineResult: XMLMappable {
var nodename: String!
var error: String?
var Linerowset: Linerowset?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
error <- map.attributes["error"]
Linerowset <- map["rowset"]
}
}
class Linerowset: XMLMappable {
var nodename: String!
var Linerows: [LineRow]?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
Linerows <- map["ROW"]
}
}
class LineRow: XMLMappable {
var nodename: String!
var Linename: String?
var nodename: String?
var LineLineID: String?
required init?(map: XMLMap) {}
func mapping(map: XMLMap) {
Linename <- map["LINENAME"]
nodename <- map["nodename"]
LineLineID <- map["LINEID"]
}
}
表视图:
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
//return LineRows.count
return LineRows.count
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
let person = LineRows[indexPath.row]
Cell.textLabel?.text = person.nodename
return Cell
}
override func prepare(for segue: uistoryboardSegue,sender: Any?) {
guard
let indexPath = tableView.indexPathForSelectedRow,let vc = segue.destination as? LineDetails
else {
return
}
let person = LineRows[indexPath.row]
vc.lineID = person.LineLineID!
}
debugprint(LineRows) 响应:
[Apiapp.LineController.LineRow,Apiapp.LineController.LineRow,Apiapp.LineController.LineRow]
dump(LineRows) 响应:
LINE1
▿ 3 elements
▿ Apiapp.LineController.LineRow #0
▿ nodename: Optional("ROW")
- some: "ROW"
▿ Linename: Optional("LINE1")
- some: "LINE1"
▿ nodename: Optional("NODE1")
- some: "NODE1"
▿ LineLineID: Optional("1")
- some: "1"
▿ Apiapp.LineController.LineRow #1
▿ nodename: Optional("ROW")
- some: "ROW"
▿ Linename: Optional("LINE1")
- some: "LINE1"
▿ nodename: Optional("NODE2")
- some: "NODE2"
▿ LineLineID: Optional("1")
- some: "1"
▿ Apiapp.LineController.LineRow #2
▿ nodename: Optional("ROW")
- some: "ROW"
▿ Linename: Optional("LINE2")
- some: "LINE2"
▿ nodename: Optional("NODE1")
- some: "NODE1"
▿ LineLineID: Optional("2")
- some: "2"