Pass data from struct array to new tableview controller

爷,独闯天下 提交于 2019-12-12 05:13:19

问题


I am trying to pass data from my Cheats struct into a new tableviewcontroller to populate the new table with the relevant info. I have done research but am new to swift. I do have experience in php but transferring my knowledge is proving quite difficult...

In my prepare for segue class i receive a few errors:

Definition conflicts with previous value:

var DataPass = CheatsArray[indexPath.row]

Cannot assign value type 'String' to '[String]':

DestViewController.CheatsArray = DataPass.name

Here is a copy of my current 3 files

Structs and arrays:

struct Game {
    var name : String
    var cheats : [Cheat]
}

struct Cheat {
    var name : String
    var code : String
    var description : String
}

// Create Our Game Info And Cheats / Codes For Each Game!
//-------------------------------------------------------
let COD4 = Game(name: "Call Of Duty 4", cheats: [Cheat(name: "Cheat", code: "Code", description: "Description")])
let GTAV = Game(name: "Grand Theft Auto 5", cheats: [Cheat(name: "Cheat", code: "Code", description: "Description")])


// Place Our New Games Inside This Array!
//---------------------------------------
let ArrayOfGames = [COD4,GTAV]

GameListController:

import Foundation
import UIKit

class GamesListViewController: UITableViewController {

    var CheatsArray = [Game]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ArrayOfGames.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = ArrayOfGames[indexPath.row].name
        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
        let DestViewController = segue.destinationViewController as! CheatsListViewController

        let DataPass : Game
        var DataPass = CheatsArray[indexPath.row]
        DestViewController.CheatsArray = DataPass.name

    }

}

CheatListController:

class CheatsListViewController: UITableViewController {

    var CheatsArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return CheatsArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
        cell.textLabel?.text = CheatsArray[indexPath.row]
        return cell

    }

}

回答1:


The first error is because you defined DataPass twice, one row after another. You can't do that in the same scope. Change the name of one.

The second error is because you cannot assign value type 'String' to '[String]'. Just looking at CheatsArray and name variables, it's clear the first is an array and the second is most likely a string. You might want to append the name to the array.



来源:https://stackoverflow.com/questions/37100873/pass-data-from-struct-array-to-new-tableview-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!