gpt4 book ai didi

ios - (Swift) fatal error : Index out of range

转载 作者:行者123 更新时间:2023-11-30 12:21:33 25 4
gpt4 key购买 nike

我有三个数组。一种显示 tableView 上每个部分的标题,一种显示标题,另一种显示每个单元格的链接。当我运行此代码时,我得到:

fatal error: Index out of range

Here is what happens after it runs

我的代码是:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var headers = ["Bein Sports", "Sky Sports", "Alkass", "Other"]

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"],
["BT Sports 1", "BT Sports 2", "Real Madrid TV", "Real Madrid TV 2"]]

var links = [["https://www.google.ca","https://www.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"],
["http://twitter.com","http://facebook.com","http://www.google.com","http://www.instagram.com"]]

var myIndex = 0

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return channels[section].count
}

func numberOfSections(in tableView: UITableView) -> Int {
return channels.count //Rows
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return headers[section] //Sections
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = channels[indexPath.section][indexPath.row] //TextLabel
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = [indexPath.section][indexPath.row]
performSegue(withIdentifier: "segue", sender: self)
}
}

在调试器中,这是突出显示的行:

myIndex = [indexPath.section][indexPath.row]

最佳答案

您可以将 myIndex 存储为 IndexPath 并使用 myIndex 来获取 indexPath 行和部分,就像这样

     var myIndex : IndexPath = IndexPath()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath
performSegue(withIdentifier: "segue", sender: self)
}

关于ios - (Swift) fatal error : Index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com