Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func sections() {
- struct LineRow {
- let lineName: String?
- let lineId: String?
- let nodeName: String
- }
- let rows: [LineRow] = [LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-1"),
- LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-2"),
- LineRow(lineName: "LINE1", lineId: "1", nodeName: "NODE1-3"),
- LineRow(lineName: "LINE2", lineId: "2", nodeName: "NODE2-1"),
- LineRow(lineName: "LINE2", lineId: "2", nodeName: "NODE2-2"),
- LineRow(lineName: "LINE3", lineId: "3", nodeName: "NODE3-1")]
- struct Sections {
- let lineId: String?
- let lineName: String?
- let nodes: [LineRow]
- }
- let sections = Dictionary(grouping: rows, by: { $0.lineId }).values.compactMap { Sections(lineId: $0.first?.lineId, lineName: $0.first?.lineName, nodes: $0) }.sorted(by: { $0.lineId ?? "" < $1.lineId ?? "" })
- sections.forEach { aSection in
- print("aSection: \(aSection.lineId)")
- aSection.nodes.forEach { print("\t \($0.nodeName)")}
- }
- //NumberOfSection: sections.count
- //NUmberOfRowsInSection: sections[section].nodes.count
- //Element at IndexPath: let person = sections[indexPath.section].nodes[indexPath.row]
- }
- sections()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement