簡體   English   中英

對象數組中的 UITableViewSections

[英]UITableViewSections from Array of Objects

我正在努力實現以下目標。 我正在將 xml 字符串映射到表格視圖中。 這部分工作正常,但是我也想在 tableview 中制作部分。 我需要 LINENAME 作為 SectionName 並且 NODENAMES 應該在這些部分的下面作為行。 下面是它應該是什么樣子的示例。

  • header (LINE1)

    • 單元格 (NODE1)
    • 單元格 (NODE2)
  • header (LINE2)

    • 單元格(節點 1)

ETC...

有人可以給我更多關於如何實現這一目標的信息嗎?

提前致謝。

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

}

Map 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!
    }

調試打印(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"

我會制作一個更適合您的表格視圖的 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: { $0.lineId }).values.compactMap { LineSections(lineId: $0.first?.lineId, lineName: $0.first?.lineName, nodes: $0) }.sorted(by: { $0.lineId ?? "" < $1.lineId ?? "" }

在您的表格視圖中填充:

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
}

不相關:以小寫字母開頭命名您的 var: let Cell => let cell等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM