【发布时间】:2020-08-02 10:12:58
【问题描述】:
我想知道为什么这段代码说索引超出范围
具体问题代码:
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 时间表[44839:2131710] 致命 错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, 第 444 行
【问题讨论】:
-
contactsAry没有足够的值...