首页 > 解决方案 > 如何解决致命错误: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 行

标签: iosswiftuiimagexcode11

解决方案


欢迎来到 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是通过rowor 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. 你可以从这里拿走。


推荐阅读