简体   繁体   中英

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. I'm getting the following error when I scroll down to the last section in UITableView and I'm using indexed UITableView .

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 18446744073709551615 beyond bounds [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.

Change numberOfSectionsInTableView to:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return mutA.count
}

Change numberOfRowsInSection to:

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

    for (var i = 0; i < 9; i++)
    {
        ...
        mutA.addObject(halfArray)
    }

Here you put 9 items in mutA.

Then you tell UITableView there are 11 sections, guess what will be thrown when you try mutA[10]?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM