(快速)致命错误:索引超出范围

编程入门 行业动态 更新时间:2024-10-25 18:31:32
本文介绍了(快速)致命错误:索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have three arrays. One displays headers for each section on the tableView, one displays titles, and one has the links for each cell. When I run this code, I get:

fatal error: Index out of range

这是什么

我的代码是:

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 = [["www.google.ca","www.facebook","facebook","facebook","facebook","facebook","facebook","facebook","facebook","facebook","facebook"], ["facebook","facebook","facebook","facebook","facebook"], ["facebook","facebook","facebook","facebook","facebook"], ["twitter","facebook","www.google","www.instagram"]] 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) } }

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

in the debugger this is the highlighted line:

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

推荐答案

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

you can store myIndex as IndexPath and using myIndex to get indexPath row and section just like this

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) }

更多推荐

(快速)致命错误:索引超出范围

本文发布于:2023-10-18 10:39:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   错误   快速

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!