【问题标题】:NSRangeException index 9 beyond bounds [0 .. 8]'NSRangeException 索引 9 超出范围 [0 .. 8]'
【发布时间】:2015-10-03 15:30:29
【问题描述】:

我正在使用NSRange 将 100 个数组分成 10 个并将它们存储在数组数组中。当我向下滚动到 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)
    }

我不知道发生了什么。

【问题讨论】:

  • 哪一行导致错误?
  • 仅供参考 - 问题标题和错误完全不同。
  • 不是任何特定的行,它显示的是 eApp delegate
  • 在调试器中添加异常断点,使其停在导致问题的行上。
  • 添加异常断点,查看代码在哪一行崩溃。

标签: ios swift uitableview


【解决方案1】:

您的cellForRowAtIndexPath 方法基于mutA 变量,但您的numberOfRowsInSectionnumberOfSectionsInTableView 方法不是。

numberOfSectionsInTableView 更改为:

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

numberOfRowsInSection 更改为:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mutA[section].count
}

【讨论】:

    【解决方案2】:
        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个section,猜猜你试试mutA[10]会抛出什么?

    【讨论】:

    • 循环添加了 9 个项目,而不是 8 个。
    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    相关资源
    最近更新 更多