Swift 行数分段错误
我得了零分,我不知道为什么。每次我选择tableViewController1并执行一个序列时,它都会崩溃并将错误发送给tableViewController2。我为故事板中的每个tableViewController设置了标识符和类。我要做的是将数据从tableViewController1传递到tableViewController2 这是来自tableViewController1的 这是tableViewController2Swift 行数分段错误,swift,uitableview,variables,segue,Swift,Uitableview,Variables,Segue,我得了零分,我不知道为什么。每次我选择tableViewController1并执行一个序列时,它都会崩溃并将错误发送给tableViewController2。我为故事板中的每个tableViewController设置了标识符和类。我要做的是将数据从tableViewController1传递到tableViewController2 这是来自tableViewController1的 这是tableViewController2 }对不起,你的方法完全错了 与创建不等于segue目标控制器
}对不起,你的方法完全错了 与创建不等于segue目标控制器的MagazineTableViewController的新实例不同,您必须实现segue的preparefor并将数据传递给segue的destination属性 将didSelectRowAt更改为 像这样实现segue的preparefor
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier.rawValue == "company" {
let destinationController = segue.destination as! MagazineTableViewController
let indexPath = sender as! IndexPath
destinationController.customInit(magazinesindex: indexPath.row, title: topics[indexPath.row])
}
}
谢谢我完全忘了准备赛格
let MagazinesData = [//an array containing arrays of magazines]
var magazinesIndex: Int!
override func viewDidLoad() {
super.viewDidLoad()
}
func customInit(magazinesindex: Int, title: String) {
self.magazinesIndex = magazinesindex // this is where the error is
self.title = title
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return MagazinesData[magazinesIndex].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MagazineTableViewCell
cell.magazineTitle?.text = MagazinesData[magazinesIndex][indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "company", sender: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier.rawValue == "company" {
let destinationController = segue.destination as! MagazineTableViewController
let indexPath = sender as! IndexPath
destinationController.customInit(magazinesindex: indexPath.row, title: topics[indexPath.row])
}
}