我使用NSRange将100的数组划分为10,并将它们存储在数组中。当我向下滚动到UITableView中的最后一节并使用索引UITableView时,我会得到以下错误。
*终止应用程序,原因:'* -__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)
}我不知道发生了什么事。
发布于 2015-10-03 15:43:06
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
}发布于 2015-10-03 15:46:49
let indexNumbers = "0 10 20 30 40 50 60 70 80 90 100"
indexOfNumbers = indexNumbers.componentsSeparatedByString(" ")在这里,您在indexOfNumbers中放置了11个字符串
for (var i = 0; i < 9; i++)
{
...
mutA.addObject(halfArray)
}在这里,您在mutA中放置了9个项目。
然后告诉UITableView有11个部分,猜猜当您尝试mutA10时会抛出什么
https://stackoverflow.com/questions/32924281
复制相似问题