numberOfRowsInSection 错误
numberOfRowsInSection Error
我得到了一个零,我不知道为什么。每次我 select tableViewController1
并执行 segue 时,它都会崩溃并将错误发送给我 tableViewController2
。我设置标识符并将 class 设置为故事板中的每个 tableViewController。我想做的是将数据从 tableViewController1
传递到 tableViewController2
本文来自tableViewController1
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let magazines = MagazineTableViewController()
magazines.customInit(magazinesindex: indexPath.row, title: topics[indexPath.row])
self.performSegue(withIdentifier: "company", sender: nil)
tableView.deselectRow(at: indexPath, animated: true)
}
这是tableViewController2
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
}
}
抱歉,您的做法完全错误。
不是创建 MagazineTableViewController
的新实例,它 不 等于 segue 的目标控制器,你必须实现 prepare(for segue
并传递数据到 segue 的 destination
属性。
将didSelectRowAt
改为
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "company", sender: indexPath)
tableView.deselectRow(at: indexPath, animated: true)
}
像这样实现prepare(for segue
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])
}
}
我得到了一个零,我不知道为什么。每次我 select tableViewController1
并执行 segue 时,它都会崩溃并将错误发送给我 tableViewController2
。我设置标识符并将 class 设置为故事板中的每个 tableViewController。我想做的是将数据从 tableViewController1
传递到 tableViewController2
本文来自tableViewController1
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let magazines = MagazineTableViewController()
magazines.customInit(magazinesindex: indexPath.row, title: topics[indexPath.row])
self.performSegue(withIdentifier: "company", sender: nil)
tableView.deselectRow(at: indexPath, animated: true)
}
这是tableViewController2
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
}
}
抱歉,您的做法完全错误。
不是创建 MagazineTableViewController
的新实例,它 不 等于 segue 的目标控制器,你必须实现 prepare(for segue
并传递数据到 segue 的 destination
属性。
将
didSelectRowAt
改为override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.performSegue(withIdentifier: "company", sender: indexPath) tableView.deselectRow(at: indexPath, animated: true) }
像这样实现
prepare(for segue
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]) } }