ios - 如何解决致命错误:Xcode Swift IOS 中的索引超出范围
问题描述
我想知道为什么这段代码说索引超出范围
具体问题代码:
cell.textLabel?.text = contactsAry[indexPath.row]
整个代码有问题:
extension ContactsViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (myContact.count + contactsAry.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < myContact.count {
let contact = myContact[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "MainUsersAccount") as!
ContactsLayout
cell.setContacts(contacts: contact)
return cell
} else {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "UserItem")
cell.textLabel?.text = contactsAry[indexPath.row]
return cell
}
}
这是我的数组“contactsAry”let contactsAry = ["Brandon", "Luisa"]
MainUserAccount 设置代码:
func createArray() -> [Contacts] {
var tempContact: [Contacts] = []
let userName = "Temp User"
let myContact = Contacts(UserIcon: UIImage(systemName: "\(userName.prefix(1).lowercased()).circle.fill")!, UserName: userName, MyAccountLabel: "My Account")
tempContact.append(myContact)
return tempContact
}
我有两个不同的原型单元试图添加到同一个 tableView。第一个基本上只是用户帐户单元格的单元格被标识为“MainUserAccount”,它只是一个可以更改图标和名称的单元格。第二个单元格被标识为“UserItem”,它基本上只是您添加到“朋友列表”中的用户列表。我不确定为什么我不断收到此错误。任何帮助将不胜感激。
我在控制台中得到的完整错误代码:
致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift,第 444 行 2020-08-02 03 :04:18.896635-0700 Schedule[44839:2131710] 致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ ContiguousArrayBuffer.swift,第 444 行
解决方案
欢迎来到 Stackoverflow!错误:
致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift,第 444 行 2020-08-02 03 :04:18.896635-0700 Schedule[44839:2131710] 致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ ContiguousArrayBuffer.swift,第 444 行
只是基本上意味着您正在访问一个数组索引,但该索引大于数组的最大索引。
如您所知,访问数组是从 0 开始的。从 an 获取索引IndexPath
是通过row
or item
, and section
。获取数组最大索引的方法是通过数据数组的计数减 1。
据我所知,就像你提到的那样,你有两种不同的细胞。您处理它的方式cellForRow
可能是正确的,但是您传递给的值numberOfRowsInSection
太多了,您正在组合两个数组的计数。
return (myContact.count + contactsAry.count)
sum - 1 将是indexPath.row
您的 tableView 将尝试访问的最大值,因此崩溃(超出范围)。
解决此问题的一种方法是利用该部分。例如,您可以这样做:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return myContact.count
} else {
return contactsAry.count
}
}
为此,您需要另一种方法numberOfSections
。
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
从那里开始,以相同的方式在cellForRow
. 你可以从这里拿走。
推荐阅读
- java - 调用 WIN10 ChromeOption 时 Selenium Grid Hub/崩溃
- scala - 如何在 Scala 中更新列中的值后重新推断 Spark Dataframe 列的数据类型
- python - 将 NLTK 块转换为字典列表
- python - 基于另一列对数据框进行分组/汇总和求和
- java - 自动助手在eclipse中不起作用?
- google-sheets - LOOKUP 在 Google 表格中的行为很奇怪
- jmeter - 将 CSV 数据集配置与 IncludeController 一起使用会导致变量未被替换
- docker - docker 环境变量不会被替换
- jquery - 如何使用 jQuery 验证插件验证 Select2 插件数组
- javascript - 如何设置 Node.js 服务器以使用 index.php 而不是默认的 index.html?