2015-08-21 28 views
2

我在運行我的應用程序時遇到了fatal error: Array index out of range錯誤,但我不明白爲什麼。這裏是我的代碼:Xcode調試器:致命錯誤:數組索引超出範圍..爲什麼?

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]串突出..爲什麼在這裏是這個錯誤?

回答

2

行數等於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] 
+0

哦確定,沒有他們不這樣做,我有更多的aSport陣列則紋波位置,這樣,我連他們出去嗎? –

+0

是的,或者你可能想要循環它們,就像顏色一樣。看到我更新的答案。 – Glorfindel

+0

太棒了!多數民衆贊成在 –

1

這似乎是rippleLocations數組的大小小於aSport ...

因此,例如,在第10行,如果aSport有10個對象,rippleLocations只有8個對象,它會崩潰。

監守在numberofRows方法你返回aSport的計數

+0

得到它,我只是連他們出來謝謝 –

相關問題