[英]NSRangeException index 9 beyond bounds [0 .. 8]'
I am using NSRange
to divide an array of 100 into 10 and storing them in array of array. 我正在使用
NSRange
将100的数组分成10并将其存储在array数组中。 I'm getting the following error when I scroll down to the last section in UITableView
and I'm using indexed UITableView
. 当我向下滚动到
UITableView
的最后一节并且使用索引的UITableView
时,出现以下错误。
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 18446744073709551615 beyond bounds [0 .. 9]'
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[__ NSArrayI objectAtIndex:]:索引18446744073709551615超出范围[0 .. 9]'
The following is my code : 以下是我的代码:
var tableData : NSMutableArray!
var mutA = NSMutableArray()
var indexOfNumbers = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableData = [
// 100 lines of string array of different starting letter
]
let indexNumbers = "0 10 20 30 40 50 60 70 80 90 100"
indexOfNumbers = indexNumbers.componentsSeparatedByString(" ")
for (var i = 0; i < 9; i++)
{
var halfArray : NSArray!
var theRange = NSRange()
theRange.location = i*10;
theRange.length = tableData.count / 10
halfArray = tableData.subarrayWithRange(theRange)
mutA.addObject(halfArray)
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return indexOfNumbers.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = mutA[indexPath.section][indexPath.row] as? String
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return indexOfNumbers
}
func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
let temp = indexOfNumbers as NSArray
return temp.indexOfObject(title)
}
I have no idea of what's going on. 我不知道发生了什么事。
Your cellForRowAtIndexPath
method is based on the mutA
variable but your numberOfRowsInSection
and numberOfSectionsInTableView
methods are not. 您的
cellForRowAtIndexPath
方法基于mutA
变量,但您的numberOfRowsInSection
和numberOfSectionsInTableView
方法却不是。
Change numberOfSectionsInTableView
to: 将
numberOfSectionsInTableView
更改为:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mutA.count
}
Change numberOfRowsInSection
to: 将
numberOfRowsInSection
更改为:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mutA[section].count
}
let indexNumbers = "0 10 20 30 40 50 60 70 80 90 100"
indexOfNumbers = indexNumbers.componentsSeparatedByString(" ")
Here you put 11 strings in indexOfNumbers 在这里,您将11个字符串放入indexOfNumbers中
for (var i = 0; i < 9; i++)
{
...
mutA.addObject(halfArray)
}
Here you put 9 items in mutA. 在这里,您将9个项目放入mutA中。
Then you tell UITableView there are 11 sections, guess what will be thrown when you try mutA[10]? 然后,您告诉UITableView有11个部分,猜想尝试mutA [10]会抛出什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.