如何从struct获取数据到TableView?



我在做Valorant App,我创建了我的structs,但我不知道如何在tableView中显示这些数据。我创建了所有的字符显示,但我不知道如何做到这一点。

这是我的结构体1。

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

这是我的字符生成结构。

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"])

]

}

这是我的TableView视图控制器

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
}




}

我只是想在tableView中反映name属性作为文本,但是我不能访问我的结构中的name和其他属性。我怎么能做到呢?

你真的很接近了:)

你已经有了与你的视图控制器相关联的代理列表,你只需要在正确的索引处找到正确的代理,你可以这样做:

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

最新更新