ios - 将数据从结构数组传递到新的 TableView Controller

标签 ios arrays swift struct tableview

我正在尝试将数据从我的 Cheats 结构传递到新的 tableviewcontroller 中,以使用相关信息填充新表。 我已经做过研究,但对 Swift 还很陌生。 我确实有 php 经验,但事实证明转移我的知识相当困难......

在我准备 segue 类(class)时,我收到一些错误:

定义与先前的值冲突:

var DataPass = CheatsArray[indexPath.row]

无法将值类型“String”分配给“[String]”:

DestViewController.CheatsArray = DataPass.name

这是我当前 3 个文件的副本

结构体和数组:

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]

游戏列表 Controller :

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

    }

}

最佳答案

第一个错误是因为您定义了 DataPass 两次,一行接着一行。你不能在同一个范围内做到这一点。更改其中一个的名称。

第二个错误是因为您无法将值类型“String”分配给“[String]”。只需查看 CheatsArrayname 变量,很明显第一个是数组,第二个很可能是字符串。您可能想要 append 数组的名称。

关于ios - 将数据从结构数组传递到新的 TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37100873/

相关文章:

ios - 从 Xcode 管理器将构建上传到 iTunes Connect 时出错

swift - 从未调用 Deinit 方法 - Swift playground

ios - 如何将查询字符串作为获取参数快速发送

iphone - 控制应用程序从后台返回时何时刷新

iphone - 参数和选择器

ios - Swift Realm - 查询空集但数据位于 RealmBrowser 中

php - unset() 将数组转换为对象

PHP 变量函数数组值

c++ - 将值均匀分布到数组中

swift - Double 类型数字的字符串格式