Проблема добавления разделов в табличное представление из данных JSON - PullRequest
0 голосов
/ 15 мая 2019

Я пытаюсь сгруппировать табличное представление, которое заполняется данными JSON.

Вот пример того, как это выглядит:

  [{"customer":"Customer1","serial":"34543453",
"rma":"P2384787","model":"M282","manufacturer":"Manufacturer1"},

 {"customer":"Customer1","serial":"13213214",
"rma":"P2384787","model":"M384","manufacturer":" Manufacturer1"},

{"customer":"Customer2","serial":"1212121323",
"rma":"P3324787","model":"M384","manufacturer":" Manufacturer1"}]

Я хотел бы сгруппировать табличное представление на основе имени клиента. Так что в моем случае это должно выглядеть так:

customer1

34543453 - Производитель1 - M282

13213214 - Производитель1 - M384

Customer2

1212121323 - Производитель1 - M384

Примечание:

Причина, по которой существует строка, разделяющая серийного производителя и модель, заключается в том, что этот разделитель в CustomerViewController.swift:

let titleStr = [item.serial, item.manufacturer, item.model].compactMap { $0 }.joined(separator: " - ")

PortfolioController.swift

import UIKit

class PortfolioController: UITableViewController {

    var portfolios = [Portfolios]()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.title = "Customer"
        fetchJSON()
    }

     func fetchJSON(){
        let urlString = "https://www.example.com/example/example.php"
        guard let url = URL(string: urlString) else { return }
        URLSession.shared.dataTask(with: url) { (data, _, error) in
            DispatchQueue.main.async {
                if let error = error {
                    print("Failed to fetch data from url", error)
                    return
                }
                guard let data = data else { return }
                do {

                    let decoder = JSONDecoder()

                    decoder.keyDecodingStrategy = .convertFromSnakeCase
                    self.portfolios = try decoder.decode([Portfolios].self, from: data)

                    self.tableView.reloadData()

                } catch let jsonError {
                    print("Failed to decode json", jsonError)
                }

            }
        }.resume()
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return portfolios.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")
        let customer = portfolios[indexPath.row]
        //cell.textLabel?.text = customer.serial


        let titleStr = [customer.serial, customer.manufacturer, customer.model].compactMap { $0 }.joined(separator: " - ")


        print(titleStr)
        // Get references to labels of cell
        cell.textLabel!.text = titleStr


        return cell
    }



}

Portfolios.swift

import UIKit

    struct Portfolios: Codable {
        let customer, serial, rma, model: String
        let manufacturer: String
    }

1 Ответ

1 голос
/ 16 мая 2019

1- Создать экземпляр var

var portfoliosDic = [String:[Portfolios]]()

2- Назначить его здесь

let res = try JSONDecoder().decode([Portfolios].self, from: data)
self.portfoliosDic = Dictionary(grouping: res, by: { $0.customer})
DispatchQueue.main.async {
 self.tableView.reloadData()
}

  override func numberOfSections(in tableView: UITableView) -> Int {
    return portfoliosDic.keys.count
  }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       let keys = Array(portfoliosDic.keys) 
       let item = portfoliosDic[keys[section]]! 
        return item.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cellId")
           let keys = Array(portfoliosDic.keys) 
           let arr = portfoliosDic[keys[indexPath.section]]! 
           let customer = arr[indexPath.row]

        //cell.textLabel?.text = customer.serial


        let titleStr = [customer.serial, customer.manufacturer, customer.model].compactMap { $0 }.joined(separator: " - ")


        print(titleStr)
        // Get references to labels of cell
        cell.textLabel!.text = titleStr


        return cell
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...