[英]NSRangeException index 9 beyond bounds [0 .. 8]'
我正在使用NSRange
将100的数组分成10并将其存储在array数组中。 当我向下滚动到UITableView
的最后一节并且使用索引的UITableView
时,出现以下错误。
*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[__ NSArrayI objectAtIndex:]:索引18446744073709551615超出范围[0 .. 9]'
以下是我的代码:
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)
}
我不知道发生了什么事。
您的cellForRowAtIndexPath
方法基于mutA
变量,但您的numberOfRowsInSection
和numberOfSectionsInTableView
方法却不是。
将numberOfSectionsInTableView
更改为:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return mutA.count
}
将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(" ")
在这里,您将11个字符串放入indexOfNumbers中
for (var i = 0; i < 9; i++)
{
...
mutA.addObject(halfArray)
}
在这里,您将9个项目放入mutA中。
然后,您告诉UITableView有11个部分,猜想尝试mutA [10]会抛出什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.