Tuesday, November 29, 2022
HomeIOS Developmentios - How can I show the json knowledge in a Swift...

ios – How can I show the json knowledge in a Swift SpreadsheetView


I’m utilizing a pod known as SpreadsheetView to indicate knowledge, from a json, in a grid however I do not know the way to present them as a result of with this pod it’s essential to invoke an array for every column.
I want to order this knowledge from my json within the corresponding columns and likewise when touching a cell in a row I used to be taken to a view to indicate the associated knowledge.

Connected the code of what I’ve accomplished.

The view i take advantage of to show the information

import UIKit
import SpreadsheetView

class TiendasViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetViewDelegate, ConsultaModeloProtocol{

let knowledge = ["Sucursal", "Venta total", "Tickets", "Piezas", "Pzs/Ticket", "Ticket prom.", "Utilidad", "Última venta"]
    let sucursales = ["Hamleys", "Bobby Brown", "Aristocrazy", "Jo Malone"]
    let vtaTotal = ["$1","$1","$1","$1"]
    let tickets = ["1","2","3","4"]
    let pzas = ["10", "20", "30", "40"]
    let pzasTicket = ["20", "40", "60", "80"]
    let tktProm = ["$2", "$4", "$6", "$8"]
    let utilidad = ["10%", "20%", "30%", "40%"]
    let ultVta = ["13:00","14:00","15:00","16:00"]
    
    var feedItems = [DetallesConsulta]()
    
    func itemConsulta(LaConsulta: [DetallesConsulta]) {
        feedItems = LaConsulta
        self.tablaTiendas.reloadData()
        
    }
    
    var selectDato : DetallesConsulta = DetallesConsulta()
    
    personal let tablaTiendas = SpreadsheetView()
    
    
    override func viewDidLoad() {
        
        tremendous.viewDidLoad()
        
        let consultaModelo = ConsultaModelo()
        consultaModelo.ElDelegado = self
        consultaModelo.downloadConsulta()
        
        tablaTiendas.dataSource = self
        tablaTiendas.delegate = self
        
        tablaTiendas.contentInset = UIEdgeInsets(high: 4, left: 0, backside: 4, proper: 0)
        tablaTiendas.intercellSpacing = CGSize(width: 4, top: 1)
        tablaTiendas.gridStyle = .none
        
        
        tablaTiendas.gridStyle = .strong(width: 1, shade: .blue)
        tablaTiendas.register(SucursalesCell.self, forCellWithReuseIdentifier: String(describing: SucursalesCell.self))
        tablaTiendas.register(DateCell.self, forCellWithReuseIdentifier: String(describing: DateCell.self))
       
        
        view.addSubview(tablaTiendas)
        print("Imprimiendo los feeditems: ", feedItems)
        // Do any further setup after loading the view.
    }
    
    override func viewDidLayoutSubviews() {
        tremendous.viewDidLayoutSubviews()
        tablaTiendas.body = CGRect(x: 0, y:216, width: view.body.measurement.width, top: view.body.measurement.height-100)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        tremendous.viewDidAppear(animated)
        tablaTiendas.flashScrollIndicators()
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
        
        if case (0...(knowledge.depend), 0) = (indexPath.column, indexPath.row) {
            
            let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: DateCell.self), for: indexPath) as! DateCell
            cell.label.textual content = knowledge[indexPath.column - 0]
            return cell
            
        } else if case(0, 1...(sucursales.depend + 1)) = (indexPath.column, indexPath.row){
            
            let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: SucursalesCell.self), for: indexPath) as! SucursalesCell
            
            cell.label.textual content = sucursales[indexPath.row - 1]
            return cell
            
        }
       
       /*
        let cell = tablaTiendas.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell
        if indexPath.row == 0 {
            cell.setup(with: knowledge[indexPath.column])
            cell.backgroundColor = .systemBlue
        }
        return cell
        */
          
      return nil

    }
    

    func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
        return knowledge.depend
    }
    
    func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
        return 1 + sucursales.depend
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
        return 200
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
        if case 0 = row{
            return 24
        }else{
            return 55
        }
        
    }
    
    func frozenColumns(in spreadsheetView: SpreadsheetView) -> Int {
        return 1
    }

}

class MyLabelCell: Cell {
    
    personal let label = UILabel()
    
    public func setup(with textual content: String){
        label.textual content = textual content
        label.textAlignment = .heart
        contentView.addSubview(label)
    }
    
    override func layoutSubviews() {
        tremendous.layoutSubviews()
        label.body = contentView.bounds
    }
}

class DateCell: Cell {
    let label = UILabel()

    override init(body: CGRect) {
        tremendous.init(body: body)

        label.body = bounds
        label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        label.font = UIFont.boldSystemFont(ofSize: 15)
        label.textAlignment = .heart

        contentView.addSubview(label)
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
}

class SucursalesCell: Cell {
    let label = UILabel()

    override init(body: CGRect) {
        tremendous.init(body: body)

        label.body = bounds
        label.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        label.font = UIFont.monospacedDigitSystemFont(ofSize: 12, weight: UIFont.Weight.medium)
        label.textAlignment = .heart

        contentView.addSubview(label)
    }

    override var body: CGRect {
        didSet {
            label.body = bounds.insetBy(dx: 6, dy: 0)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        tremendous.init(coder: aDecoder)
    }
}

The mannequin to obtain the json

import UIKit
import Basis


protocol ConsultaModeloProtocol: AnyObject {
    func itemConsulta (LaConsulta: [DetallesConsulta])
}

var fechaPresente: String = ""
var fechaPasada: String = ""


let elToken : String = UserDefaults.customary.string(forKey: "token")!
let helper = Helper()

class ConsultaModelo: NSObject {
    
    weak var ElDelegado : ConsultaModeloProtocol!
    
    let URLPath = helper.host+"tiendas"
    
    func downloadConsulta(){
        
        var request = URLRequest(url: URL(string: URLPath)!)
        request.addValue("utility/json", forHTTPHeaderField: "Settle for")
        request.addValue("utility/json", forHTTPHeaderField: "Content material-Kind")
        request.addValue("Bearer (elToken)", forHTTPHeaderField: "Authorization")
        
        request.httpMethod = "POST"
        
        let SessionDefault = Basis.URLSession(configuration: URLSessionConfiguration.ephemeral)
        URLCache.shared.removeAllCachedResponses()
        
        let process = SessionDefault.dataTask(with: request){
            (knowledge, response, error)in
            if error != nil {
                print("Error al descargar la consulta")
            }else{
                print("Datos descargados")
                self.parseJSON(knowledge!)
            }
        }
        process.resume()
    }
    
    func parseJSON(_ knowledge: Knowledge){
        var jsonResult = NSArray()
        
        do{
            jsonResult = attempt JSONSerialization.jsonObject(with: knowledge, choices: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
        }catch let error as NSError{
            print(error)
        }
        
        var jsonElement = NSDictionary()
        var detalles = [DetallesConsulta]()
        
        for i in 0 ..< jsonResult.depend{
            
            jsonElement = jsonResult[i] as! NSDictionary
            let detalle = DetallesConsulta()
            
            let Fecha = jsonElement["Fecha"]
            let Sucursal = jsonElement["Sucursal"]
            let Suc = jsonElement["Suc"]
            let VentaTotal = jsonElement["Venta_Total"]
            let NoFolios = jsonElement["N_Folios"]
            let Piezas = jsonElement["Piezas"]
            let PzaxTicket = jsonElement["PzaxTicket"]
            let TicketPromedio = jsonElement["TicketPromedio"]
         
            detalle.Fecha = Fecha as? String
            detalle.Sucursal = Sucursal as? String
            detalle.Suc = Suc as? String
            detalle.VentaTotal = VentaTotal as? String
            detalle.NoFolios = NoFolios as? Int
            detalle.Piezas = Piezas as? String
            detalle.PzaxTicket = PzaxTicket as? String
            detalle.TicketPromedio = TicketPromedio as? String
            
            detalles.append(detalle)
            
        }
        
        DispatchQueue.principal.async(execute: { ()-> Void in
            
            self.ElDelegado.itemConsulta(LaConsulta: detalles)
        })
        
    }
 
    
    
}```

The small print


class DetallesConsulta: NSObject {
    
    var Fecha: String?
    var Sucursal: String?
    var Suc: String?
    var VentaTotal: String?
    var NoFolios: Int?
    var Piezas: String?
    var PzaxTicket: String?
    var TicketPromedio: String?
    
    override init(){
        
    }
    
    init(Fecha: String, Sucursal: String, Suc: String, VentaTotal: String, NoFolios: Int, Piezas: String, PzaxTicket: String, TicketPromedio: String){
        self.Fecha = Fecha
        self.Sucursal = Sucursal
        self.Suc = Suc
        self.VentaTotal = VentaTotal
        self.NoFolios = Int(NoFolios)
        self.Piezas = Piezas
        self.PzaxTicket = PzaxTicket
        self.TicketPromedio = TicketPromedio
        
    }
    override var description: String{
        
        return "Fecha: (Fecha), Sucursal: (Sucursal), Suc: (Suc), VentaTotal: (VentaTotal), NoFolios: (NoFolios), Piezas: (Piezas), PzaxTicket: (PzaxTicket), TicketPromedio: (TicketPromedio)"
        
    }
    
}```


I hope you possibly can assist me.

Thanks

I downloaded the information from the json and want to have it displayed in a grid desk with this pod.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments