简体   繁体   English

NSRangeException索引9超出范围[0 .. 8]'

[英]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变量,但您的numberOfRowsInSectionnumberOfSectionsInTableView方法却不是。

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.

相关问题 NSRangeException-索引(2008)超出范围(1) - NSRangeException - Index (2008) beyond bounds (1) &#39;NSRangeException&#39;,原因:&#39;***-[__ NSArrayM objectAtIndexedSubscript:]:索引1超出范围[0 .. 0]&#39; - 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 1 beyond bounds [0 .. 0]' &#39;NSRangeException&#39;,原因:&#39;* -[__NSArrayM objectAtIndex:]:索引 2 超出范围 [0 .. 1]&#39; - 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' NSRangeException。 对于空NSArray索引0超出范围 - NSRangeException. For empty NSArray index 0 beyond bounds 致命异常:NSRangeException - 索引 2 超出范围 [0 .. 1] - Fatal Exception: NSRangeException - index 2 beyond bounds [0 .. 1] NSRangeException与NSArray objectAtIndex,索引超出范围 - NSRangeException with NSArray objectAtIndex, index beyond bounds &#39;NSRangeException&#39;,原因:&#39;*** - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]&#39; - 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' UITableView 边界终止应用程序 &#39;NSRangeException&#39; -[__NSArrayM objectAtIndex:]: 索引 2 超出边界 [0 .. 1]&#39; - UITableView bounds Terminating app 'NSRangeException' -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' NSRangeException&#39;,原因:&#39;*** -[__NSArrayM objectAtIndex:]:索引 5 超出空数组的边界&#39; - NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds for empty array' iOS Parse&#39;NSRangeException&#39;,原因:&#39;***-[__ NSArrayI objectAtIndex:]:索引1超出范围[0 .. 0] - iOS Parse 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM