首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSRangeException索引9超出界限[0 .[ 8]‘

NSRangeException索引9超出界限[0 .[ 8]‘
EN

Stack Overflow用户
提问于 2015-10-03 15:30:29
回答 2查看 2.4K关注 0票数 0

我使用NSRange将100的数组划分为10,并将它们存储在数组中。当我向下滚动到UITableView中的最后一节并使用索引UITableView时,我会得到以下错误。

*终止应用程序,原因:'* -__NSArrayI objectAtIndex::索引18446744073709551615超出界限0。9‘

以下是我的代码:

代码语言:javascript
复制
    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)
    }

我不知道发生了什么事。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-03 15:43:06

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

numberOfSectionsInTableView更改为:

代码语言:javascript
复制
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return mutA.count
}

numberOfRowsInSection更改为:

代码语言:javascript
复制
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return mutA[section].count
}
票数 2
EN

Stack Overflow用户

发布于 2015-10-03 15:46:49

代码语言:javascript
复制
    let indexNumbers = "0 10 20 30 40 50 60 70 80 90 100"
    indexOfNumbers = indexNumbers.componentsSeparatedByString(" ")

在这里,您在indexOfNumbers中放置了11个字符串

代码语言:javascript
复制
    for (var i = 0; i < 9; i++)
    {
        ...
        mutA.addObject(halfArray)
    }

在这里,您在mutA中放置了9个项目。

然后告诉UITableView有11个部分,猜猜当您尝试mutA10时会抛出什么

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32924281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档