【问题标题】:Xcode Debugger: fatal error: Array index out of range.. why?Xcode 调试器:致命错误:数组索引超出范围.. 为什么?
【发布时间】:2015-11-15 04:01:08
【问题描述】:

我在运行我的应用程序时收到fatal error: Array index out of range 错误,但我不明白为什么。这是我的代码:

  var rippleLocations: [MKRippleLocation] = [.TapLocation, .TapLocation, .Center, .Left, .Right, .TapLocation, .TapLocation, .TapLocation]
    var circleColors = [UIColor.clearColor(), UIColor.clearColor(),UIColor.clearColor(),UIColor.clearColor()]

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return aSport.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MKTableViewCell
        cell.textLabel?.text = aSport[indexPath.row].name
        cell.textLabel?.text = aSport[indexPath.row].name
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 16)
        cell.textLabel?.textColor = UIColor.whiteColor()
        cell.rippleLocation = rippleLocations[indexPath.row]
        let index = indexPath.row % circleColors.count
        cell.rippleLayerColor = circleColors[index]

        return cell
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let cell = sender as! MKTableViewCell
        let row = tableView.indexPathForCell(cell)?.row
        let detail = segue.destinationViewController as! SecondTableViewController
        detail.selectedSchool = aSport[row!]
    }

错误在cell.rippleLocation = rippleLocations[indexPath.row] 字符串上突出显示。为什么会出现此错误?

【问题讨论】:

    标签: ios xcode swift uitableview cell


    【解决方案1】:

    行数等于aSport.count

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return aSport.count
    }
    

    不应该是这样的

        return rippleLocations.count
    

    ?或者你确定aSportrippleLocations 总是有相同数量的元素?如果要循环浏览波纹位置,请替换

    cell.rippleLocation = rippleLocations[indexPath.row]
    

    cell.rippleLocation = rippleLocations[indexPath.row % rippleLocations.count]
    

    【讨论】:

    • 哦,好吧,不,他们没有,我有更多的 aSport 数组,然后是波纹位置,所以我应该把它们平掉吗?
    • 是的,或者您可能想要循环浏览它们,就像使用颜色一样。请参阅我的更新答案。
    • 太棒了!其实这样更好
    【解决方案2】:

    rippleLocations 数组的大小似乎小于 aSport ...

    因此,例如在第 10 行,如果 aSport 有 10 个对象,而ippleLocations 只有 8 个对象,它将崩溃。

    因为在 numberofRows 方法中您返回的是 aSport 的计数

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多