【问题标题】:How to solve Fatal error: Index out of range in Xcode Swift IOS如何解决致命错误:Xcode Swift IOS 中的索引超出范围
【发布时间】: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 没有足够的值...

标签: ios swift uiimage xcode11


【解决方案1】:

您可以做的是删除珍贵数组的计数...否则它将尝试访问第 3 个实例而不是 0

let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "UserItem")
        cell.textLabel?.text = contactsAry[indexPath.row - myContact.count]

【讨论】:

    【解决方案2】:

    欢迎来到 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 时间表[44839:2131710] 致命 错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, 第 444 行

    基本上意味着您正在访问一个数组索引,但该索引大于数组的最大索引。


    如您所知,访问数组是从 0 开始的。 从IndexPath 获取索引是通过rowitemsection。 获取数组最大索引的方法是通过数据数组的计数减 1。


    据我所知,就像你提到的那样,你有两种不同的细胞。您在 cellForRow 中处理它的方式可能是正确的,但是您传递给 numberOfRowsInSection 的值太多了,您正在合并两个数组的计数。

    return (myContact.count + contactsAry.count)

    总和 - 1 将是您的 tableView 将尝试访问的最大 indexPath.row,因此崩溃(超出范围)。


    解决此问题的一种方法是利用该部分。例如,您可以这样做:

    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 中以相同的方式处理部分和行。你可以从这里拿走。

    【讨论】:

    • 这个帮助我同时放置了两个内容,但我仍然无法显示完整内容。也谢谢你欢迎我。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    相关资源
    最近更新 更多