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

上面的代码是我目前在测试项目中的swift文件中的代码。
我现在尝试从上面获取值来填充tableview,请参见以下内容:
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
    }

}

但我收到一个错误:
“无法将类型值'Games'分配给类型'String?'”
我是斯威夫特的新手,但确实有php方面的经验,我正在努力将我的知识转移到:(
我很感激你的帮助。
致以最诚挚的问候
罗里

最佳答案

细胞类型为textLabel?.text。您正在尝试为其分配一个String

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

您需要从Game对象创建一个字符串,描述您的游戏。最简单的解决方案是使用Game
cell.textLabel?.text = ArrayOfGames[indexPath.row].GameName

这将编译并运行。手机的标签将与你的游戏名称相对应。
一个更有趣的描述可以由一个作弊列表组成:
let cheatList = ArrayOfGames[indexPath.row]
    .GameCheats
    .map { "\($0.CheatName): \($0.CheatCode) \($0.CheatDescription)" }
    .joinWithSeparator(", ")
cell.textLabel?.text = "\(ArrayOfGames[indexPath.row].GameName) \(cheatList)"

09-30 15:36
查看更多