简体   繁体   中英

PopulateTableview via Structs & Arrays

    struct Games {
    var GameName        :   String
    var GameCheats      :   [Cheats]
}

struct Cheats {
    var CheatName           :   String
    var CheatCode           :   String
    var CheatDescription    :   String
}

let COD4 = Games(GameName: "Name", GameCheats: [Cheats(CheatName: "Cheat", CheatCode: "Code", CheatDescription: "Description")])

The above code is what i currently have in a swift file within my test project. I am now trying to take values from above to populate a tableview, see below:

class GamesListViewController: UITableViewController {

    var ArrayOfGames = [COD4]

    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]
        return cell
    }

}

But i am receiving an error: " Cannot assign type value 'Games' to type 'String?' "

I am new to swift but do have experience in php, I'm seriously struggling to transfer my knowledge across :(

I appreciate any help.

Kindest regards Rory

Cell's textLabel?.text is of type String . You are trying to assign a Game to it:

cell.textLabel?.text = ArrayOfGames[indexPath.row]

You need to create a string from your Game object, describing your game. The simplest solution would be to use name :

cell.textLabel?.text = ArrayOfGames[indexPath.row].GameName

This would compile and run. Cell's label would correspond to the name of your game.

A more interesting description can be formed with a list of cheats:

let cheatList = ArrayOfGames[indexPath.row]
    .GameCheats
    .map { "\($0.CheatName): \($0.CheatCode) \($0.CheatDescription)" }
    .joinWithSeparator(", ")
cell.textLabel?.text = "\(ArrayOfGames[indexPath.row].GameName) \(cheatList)"

ArrayOfGames[indexPath.row] returns a Games struct. You cannot assign that to the text property of a label (it expects a String? ). I assume you meant to use the GameName property on your struct. For example:

cell.textLabel?.text = ArrayOfGames[indexPath.row].GameName

But more importantly, you should consider re-thinking your namings. A few conventions that you should follow are:

  • Property & method names should begin with a lowercase letter, and follow camelCase

  • Class, struct, enum & protocol names should begin with an uppercase letter and follow CamelCase

  • Class and struct names should be singular unless they actually represent something plural (ie not Games if it only represents a single game)

  • You should avoid repeating the name of your struct in its properties (ie gameName in your Game struct)

  • You should avoid repeating the type of a property in its name if it already has a clear meaning (ie games , not arrayOfGames )

Therefore a more conventional naming system for your code would look like this:

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

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


class GamesListViewController: UITableViewController {

    var games = [game1, game2, game3, ... gameN]

    ...


   cell.textLabel?.text = games[indexPath.row].name

Ray Wenderlich has a pretty nice Swift style guide that I'd recommend you have a look at.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM