Objects 数组中的 UITableViewSections
UITableViewSections from Array of Objects
我正在努力实现以下目标。我正在将 xml 字符串映射到表视图中。这部分工作正常,但我也想在表格视图中制作部分。我需要 LINENAME 成为 SectionName,NODENAMES 应该作为行位于这些部分的下方。下面是它的外观示例。
header (LINE1)
- 单元格 (NODE1)
- 单元格 (NODE2)
header (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 字符串映射到 Objects
的数组
// 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!
}
调试打印(LineRows)响应:
[ApiApp.LineController.LineRow, ApiApp.LineController.LineRow, ApiApp.LineController.LineRow]
转储(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"
我会制作一个更适合您的 table 视图的 ViewModel。
struct LineSections {
let lineId: String?
let lineName: String?
let nodes: [LineRow]
}
我猜你在某个地方有 var LineRows: [LineRows] = []
,作为你 ViewController 的 属性。我会用 var lineSections: [LineSections] = []
.
来改变它
然后,
self.LineRows = food!.Linerowset?.Linerows ?? []
let rows = food?.Linerowset?.Linerows ?? []
self.lineSections = Dictionary(grouping: rows, by: { [=12=].lineId }).values.compactMap { LineSections(lineId: [=12=].first?.lineId, lineName: [=12=].first?.lineName, nodes: [=12=]) }.sorted(by: { [=12=].lineId ?? "" < .lineId ?? "" }
在您的 table 视图中填充:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lineSections[section].node.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return lineSections.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let person = lineSections[indexPath.section].node[indexPath.row]
cell.textLabel?.text = person.Nodename
return cell
}
不相关:以小写字母命名您的变量:let Cell
=> let cell
等
我正在努力实现以下目标。我正在将 xml 字符串映射到表视图中。这部分工作正常,但我也想在表格视图中制作部分。我需要 LINENAME 成为 SectionName,NODENAMES 应该作为行位于这些部分的下方。下面是它的外观示例。
header (LINE1)
- 单元格 (NODE1)
- 单元格 (NODE2)
header (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 字符串映射到 Objects
的数组// 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!
}
调试打印(LineRows)响应:
[ApiApp.LineController.LineRow, ApiApp.LineController.LineRow, ApiApp.LineController.LineRow]
转储(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"
我会制作一个更适合您的 table 视图的 ViewModel。
struct LineSections {
let lineId: String?
let lineName: String?
let nodes: [LineRow]
}
我猜你在某个地方有 var LineRows: [LineRows] = []
,作为你 ViewController 的 属性。我会用 var lineSections: [LineSections] = []
.
然后,
self.LineRows = food!.Linerowset?.Linerows ?? []
let rows = food?.Linerowset?.Linerows ?? []
self.lineSections = Dictionary(grouping: rows, by: { [=12=].lineId }).values.compactMap { LineSections(lineId: [=12=].first?.lineId, lineName: [=12=].first?.lineName, nodes: [=12=]) }.sorted(by: { [=12=].lineId ?? "" < .lineId ?? "" }
在您的 table 视图中填充:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lineSections[section].node.count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return lineSections.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let person = lineSections[indexPath.section].node[indexPath.row]
cell.textLabel?.text = person.Nodename
return cell
}
不相关:以小写字母命名您的变量:let Cell
=> let cell
等