iOS - 索引超出范围

标签 ios swift uitableview

我在我的应用程序中偶然发现了一个错误:

fatal error: Index out of range (lldb)

我想我可能知道问题出在哪里,但是不知道如何修改错误。

我认为是因为我使用的是节标题,所以这是导致问题的原因。我已经校对过代码并尝试修复它并在线搜索。下面我发布了我的代码示例(不想全部包含,因为它包含几百行代码)。

本质上,我将 TableViewController 与 SWReveal 结合使用,用户在其中选择一个选项并显示文本。

class BackTableVC: UITableViewController {


    struct Brands {
      var sectionName : String!
        var sectionBrands : [String]!
    }


    struct ThirdView {
        var ThirdViewArray = [String]()
    }

    var brandArray = [Brands]()



    var ThirdArray = [ThirdView]()

    var brandAnswerArray = [String]()

    override func viewDidLoad() {

        brandArray = [


            Brands(sectionName: "Bugatti", sectionBrands: ["EB 110","Veyron"])]

        ThirdArray = [ThirdView(ThirdViewArray: ["EB 110","Veyron"])]



 }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  brandArray[section].sectionBrands.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!

        cell.textLabel?.text = brandArray[indexPath.section].sectionBrands[indexPath.row]

        return cell

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let DestVC = segue.destinationViewController as! CarDetailsVC

        let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

        let ThirdAnswerArray : ThirdView

        ThirdAnswerArray = ThirdArray[indexPath.row]

        DestVC.brandAnswerArray = ThirdAnswerArray.ThirdViewArray

       DestVC.FirstString = brandAnswerArray[indexPath.row]

    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return brandArray.count
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return brandArray[section].sectionName

    }


}
    import Foundation

struct ThirdView {
    var ThirdViewArray = [String]()
}

    class CarDetailsVC: UIViewController {


       var FirstString = String()

    var brandAnswerArray = [String]()






    @IBOutlet var Label: UILabel!

    override func viewDidLoad() {

        Label.text = FirstString

    }

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            _ = segue.destinationViewController as! CarDetailsVC            
        }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
}


}

我的 ThirdView 结构和 CarDetailsVC 位于单独的 .swift 文件中。

让我伤心的是:

       DestVC.FirstString = brandAnswerArray[indexPath.row]

附言如果我要这样做:

DestVC.FirstString = "Hello World"

仅选择第一个选项时显示 Hello World,然后代码/应用程序中断,我在该行收到相同的错误“索引超出范围”:

        ThirdAnswerArray = ThirdArray[indexPath.row]

最佳答案

这个简单的答案是您的 brandAnswerArray 没有足够的值来为您提供索引 indexPath.row 处的内容。即,如果您有一个包含 5 个值的数组,并且您向它请求数组 [8],应用程序将崩溃,因为索引 8 不存在。

具体来说,您是在告诉表格您有一定数量的单元格/行:

brandArray[section].sectionBrands.count

这意味着对于从 0 到 brandArray[section].sectionBrands.count 的每个整数,表格将要求您生成一个单元格。因此,这就是您的 indexPath.row 可以拥有的范围。

但是:在您的 prepareForSegue 中,您正在访问 brandAnswerArray[indexPath.row],而 brandAnswerArray 根本没有足够的值来为您提供该请求索引处的任何内容(这是一种风险,因为您使用了不同的部分建表的数据)。

关于iOS - 索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38927866/

相关文章:

iphone - MPMoviePlayerController : Player hides Player Controls permenantly only in blow iOS6

ios - 向 TextView 添加约束在 Swift 中不起作用

iphone - 如何比较自定义 UITableViewCel 中的标签是否为空?

ios - Xcode 自动布局 Storyboard预览不显示容器 View

ios - 检测视网膜显示

ios - 尝试使用轻量级迁移更新我的 DBModel 但没有任何反应

arrays - swift 发现 fatal error 为零

html - 如何在 Swift 3 中加载带有 HTML 的 CSS 文件?

ios - 更改 UITableView 删除图像 swit

ios - UITableView - 在编辑模式下重复使用单元格