Home > Net >  How to fetch data from struct to TableView?
How to fetch data from struct to TableView?

Time:01-19

So I'm doing Valorant App, I created my structs, but I couldn't figure out how to show this data in tableView. I created all characters for showing but i cant figure out how can i do that.

This is my struct one.

import Foundation

struct Agent: Hashable{
  
    
    let name: String
    let type: AgentType
    let origin: String
    let abilities: [String]
 //   let iconAgent : [UIImage]
}

enum AgentType:String{
    case controller = "Controller"
    case sentinel = "Sentinel"
    case initiator = "Initiator"
    case duelist = "Duelist"
}

This is my character generating struct.

struct ValorantReferenceApp {
    var agents: [Agent] =  [
        Agent(name: "Brimstone", type: .controller, origin: "United States", abilities: [   "Incendiary",
                                                                                    "Stim Beacon",
                                                                                         "Sky Smoke",
                                                                                         "Orbital Strike"]),
        Agent(name: "Viper", type: .controller, origin: "United States", abilities: ["Snake Bite",
                                                                                     "Poison Cloud",
                                                                                     "Toxic Screen",
                                                                                     "Viper's Pit"]),
        Agent(name: "Omen", type: .controller, origin: "Unknown", abilities: [
                                                                          "Shrouded Step",
                                                                              "Paranoia",
                                                                              "DarkCover",
                                                                       "FromtheShadows"]),
        Agent(name: "Killjoy", type: .sentinel, origin: "Germany", abilities: ["Alarmbot",
                                                                               "Nanoswarm",
                                                                               "Turret",
                                                                               "Lockdown"]),
        Agent(name: "Cypher", type: .sentinel, origin: "Morocco", abilities: ["Trapwire",
                                                                              "Cyber Cage",
                                                                              "Spycam",
                                                                              "Neural Theft"]),
        Agent(name: "Sova", type: .initiator, origin: "Russia", abilities: ["Owl Drone",
                                                                            "Shock Bolt",
                                                                            "Recon Bolt",
                                                                            "Hunter's Fury"]),
        Agent(name: "Sage", type: .sentinel, origin: "China", abilities: ["Barrier Orb",
                                                                          "Slow Orb",
                                                                          "Healing Orb",
                                                                          "Resurrection"]),
        Agent(name: "Phoenix", type: .duelist, origin: "United Kingdom", abilities:         ["Blaze",
                                                                                          "Curveball",
                                                                                     "HotHands",
                                                                                     "Runit Back"]),
        Agent(name: "Jett", type: .duelist, origin: "South Korea", abilities: ["Cloudburst",
                                                                               "Updraft",
                                                                               "Tailwind",
                                                                               "Blade Storm"]),
        Agent(name: "Reyna", type: .duelist, origin: "Mexico", abilities: ["Leer",
                                                                           "Devour",
                                                                           "Dismiss",
                                                                           "Empress"]),
        Agent(name: "Raze", type: .duelist, origin: "Brazil", abilities: ["Boom Bot",
                                                                          "Blast Pack",
                                                                          "Paint Shells",
                                                                          "Showstopper"]),
        Agent(name: "Breach", type: .initiator, origin: "Sweden", abilities:  
                                                                            ["Aftershock",
                                                                             "Flashpoint",
                                                                              "FaultLine",
                                                                    "Rolling   Thunder"]), 

        Agent(name: "Skye", type: .initiator, origin: "Australia", abilities: ["Regrowth", 
                                                                            "Trailblazer",                                                                                      
                                                                          " GuidingLight",   
                                                                              "Seekers"]),
        Agent(name: "Yoru", type: .duelist, origin: "Japan", abilities: ["Fakeout",
                                                                         "Blindside",
                                                                         "Gatecrash",
                                                                         "Dimensional Drift"]),
        Agent(name: "Astra", type: .controller, origin: "Ghana", abilities: [
                                                                            "GravityWell",
                                                                            "Nova Pulse",
                                                                       "Nebula/Dissipate",
                                                                             "Astral Form"]),
        Agent(name: "KAYO", type: .initiator, origin: "AT Earth", abilities: ["FRAG/ment",
                                                                            "FLASH/drive",
                                                                             "ZERO/point",
                                                                              "NULL/cmd"]),
        Agent(name: "Chamber", type: .sentinel, origin: "France", abilities: ["Trademark",
                                                                             "Headhunter",
                                                                             "Rendezvous",
                                                                              "Tour De Force"]),
        Agent(name: "Neon", type: .duelist, origin: "Philippines", abilities: ["FastLane",
                                                                              "RelayBolt",
                                                                               "HighGear",
                                                                               "Overdrive"]),
        Agent(name: "Fade", type: .initiator, origin: "Turkey", abilities: ["Prowler",
                                                                            "Seize",
                                                                            "Haunt",
                                                                            "Nightfall"])
            
    ]
   
}

This im my TableView view controller.

import UIKit

class AgentListVC: UIViewController {
    
    var agentsReference = ValorantReferenceApp().agents
    
    var agentProperties : Agent?


    @IBOutlet var tableView: UITableView!
    
    override func viewDidLoad() {
        tableView.delegate = self
        tableView.dataSource = self
        super.viewDidLoad()
 
    }


}

extension AgentListVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return agentsReference.count
       
       
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! AgentCellVC
       //  cell.agentNameLAbel.text = agentProperties?.name
     return cell
    }
    
    
    
    
}

I simply want to reflect the name property as text in the tableView, but I cannot access the name and other properties in my structs. How can i do that?

CodePudding user response:

You are really close :)

You already have the list of agents associated with your view controller, you just need to find the correct agent at the right index, you can do that like so:

let agent = agentsReference[indexPath.row]
cell.agentNameLAbel.text = agent.name
  • Related