[英]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.