使用 segue 从结构数组传递值:索引超出范围
Passing value from struct array with segue : index out of range
希望这是我要问的最后一个问题!
我已经研究了 48 小时了,但仍然找不到答案。
这是我使用的代码:
DataSource.swift:
struct Game {
var name : String
var cheats : [Cheat]
}
struct Cheat {
var name : String
var code : String
var desc : String
}
GameListViewController.swift
import Foundation
import UIKit
class GameListViewController: UITableViewController {
var gamesArray = [Game]()
var cheatsArray = [Cheat]()
override func viewDidLoad() {
super.viewDidLoad()
gamesArray = [Game(name: "Game1", cheats: [Cheat(name: "cheat1", code: "code1", desc: "desc1")])]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gamesArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
cell.textLabel?.text = gamesArray[indexPath.row].name
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let DestViewController = segue.destinationViewController as! CheatListViewController
var DataPass : Cheat
DataPass = cheatsArray[indexPath.row]
DestViewController.cheatnameArray = DataPass.name
var DataPass2 : Cheat
DataPass2 = cheatsArray[indexPath.row]
DestViewController.cheatcodeArray = DataPass2.code
var DataPass3 : Cheat
DataPass3 = cheatsArray[indexPath.row]
DestViewController.cheatdescArray = DataPass3.desc
}
}
CheatListViewController.swift
class CheatListViewController: UITableViewController {
var cheatcodeArray = String()
var cheatnameArray = String()
var cheatdescArray = String()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
当我从 gamesArray select "Game 1" 时,我立即收到来自 "DataPass" 的第一个实例的索引超出范围错误。
我以这种方式构建了我的数据源,这样我就不必单独编辑数组并保持我的对象整洁。
如果有人能指出正确的方向,我将永远感激不已!
亲切的问候
罗里
对我来说,你似乎没有用任何作弊填充你的 cheatsArray
变量。这就是您收到索引超出范围异常的原因。
从你的代码中很难理解你想要实现的目标,但我想我已经做到了..
请注意,我使用可选绑定来解包 destinationViewController
,这是安全的,因为执行的任何其他 segue 也会触发相同的 prepareForSegue
。
if let destViewController = segue.destinationViewController as? CheatListViewController {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
var game = gamesArray[indexPath.row]
destViewController.cheatnameArray = game.cheats.map({ [=10=].name })
destViewController.cheatcodeArray = game.cheats.map({ [=10=].code })
destViewController.cheatdescArray = game.cheats.map({ [=10=].desc })
}
将数组更改为实际的字符串数组而不是字符串..
var cheatcodeArray = [String]()
var cheatnameArray = [String]()
var cheatdescArray = [String]()
希望这是我要问的最后一个问题! 我已经研究了 48 小时了,但仍然找不到答案。
这是我使用的代码:
DataSource.swift:
struct Game {
var name : String
var cheats : [Cheat]
}
struct Cheat {
var name : String
var code : String
var desc : String
}
GameListViewController.swift
import Foundation
import UIKit
class GameListViewController: UITableViewController {
var gamesArray = [Game]()
var cheatsArray = [Cheat]()
override func viewDidLoad() {
super.viewDidLoad()
gamesArray = [Game(name: "Game1", cheats: [Cheat(name: "cheat1", code: "code1", desc: "desc1")])]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gamesArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell!
cell.textLabel?.text = gamesArray[indexPath.row].name
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let DestViewController = segue.destinationViewController as! CheatListViewController
var DataPass : Cheat
DataPass = cheatsArray[indexPath.row]
DestViewController.cheatnameArray = DataPass.name
var DataPass2 : Cheat
DataPass2 = cheatsArray[indexPath.row]
DestViewController.cheatcodeArray = DataPass2.code
var DataPass3 : Cheat
DataPass3 = cheatsArray[indexPath.row]
DestViewController.cheatdescArray = DataPass3.desc
}
}
CheatListViewController.swift
class CheatListViewController: UITableViewController {
var cheatcodeArray = String()
var cheatnameArray = String()
var cheatdescArray = String()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
当我从 gamesArray select "Game 1" 时,我立即收到来自 "DataPass" 的第一个实例的索引超出范围错误。 我以这种方式构建了我的数据源,这样我就不必单独编辑数组并保持我的对象整洁。
如果有人能指出正确的方向,我将永远感激不已!
亲切的问候 罗里
对我来说,你似乎没有用任何作弊填充你的 cheatsArray
变量。这就是您收到索引超出范围异常的原因。
从你的代码中很难理解你想要实现的目标,但我想我已经做到了..
请注意,我使用可选绑定来解包 destinationViewController
,这是安全的,因为执行的任何其他 segue 也会触发相同的 prepareForSegue
。
if let destViewController = segue.destinationViewController as? CheatListViewController {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
var game = gamesArray[indexPath.row]
destViewController.cheatnameArray = game.cheats.map({ [=10=].name })
destViewController.cheatcodeArray = game.cheats.map({ [=10=].code })
destViewController.cheatdescArray = game.cheats.map({ [=10=].desc })
}
将数组更改为实际的字符串数组而不是字符串..
var cheatcodeArray = [String]()
var cheatnameArray = [String]()
var cheatdescArray = [String]()