【发布时间】:2017-06-23 03:11:12
【问题描述】:
我有三个不同的数组来保存信息。一个显示部分标题,一个显示每个单元格的标题,一个提供到另一个 viewController 的链接。顺便说一句,我正在使用 Swift。
但是当我运行它时,所有单元格都使用数组中的第一个 url,而不是其余的 url。
这是我的代码:
import UIKit
class FirstViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate {
var headers = ["Bein Sports", "Sky Sports", "Alkass"]
var channels = [["Bein Sports 1","Bein Sports 2","Bein Sports 3","Bein Sports 4","Bein Sports 5","Bein Sports 6","Bein Sports 7","Bein Sports 8","Bein Sports 9","Bein Sports 10","Bein Sports News"],
["Sky Sports 1","Sky Sports 2","Sky Sports 3","Sky Sports 4","Sky Sports 5"], ["Alkass One", "Alkass Two", "Alkass Three", "Alkass Four", "Alkass Five"]]
var links = ["https://google.ca","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"]
var myIndex: IndexPath?
func numberOfSections(in tableView: UITableView) -> Int {
return headers.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return channels[section].count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return headers[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = channels[indexPath.section][indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath
performSegue(withIdentifier: "segue", sender: self)
}
}
【问题讨论】:
-
究竟是哪一行代码导致了错误?
-
我相信有人会发现你做错了什么,但这可能是最容易调试的问题。您应该借此机会学习使用调试器。一旦您的应用程序崩溃,请阅读导致它的内容的内容,您会发现问题所在。如果您无法使用调试器来查找此问题的原因,那么您在开发任何类型的应用程序时都不会走得太远。
-
myIndex = [indexPath.section][indexPath.row] 根据调试器,这行似乎有问题(显示为绿色)。
-
@KarrarAl-Mimar 将您的声明更改为
var myIndex: IndexPath? -
并将其设置在那里
myIndex = indexPath
标签: ios arrays swift uitableview