[__NSOrderedSetM objectAtIndex:]:索引10超出界限[0 .. 9 [

时间:2015-10-13 16:20:40

标签: ios swift

我将包含我的表视图中使用的所有JSON对象的主数据结构从NSMutableArray更改为NSMutableOrderedSet。现在,我的应用程序正确地从API加载前十个对象,但第二个我想向下滚动,它崩溃了。有什么建议吗?

override func scrollViewDidScroll(scrollView: UIScrollView) {
    if (scrollView.contentOffset.y + view.frame.size.height > scrollView.contentSize.height * 0.8) {
        populateEditorials()
    }
}

func populateEditorials() {
    if populatingEditorials {
        return
    }

    populatingEditorials = true


    Alamofire.request(GWNetworking.Router.Editorials(self.currentPage)).responseJSON() { response in
        if let JSON = response.result.value {
           /*
        if response.result.error == nil {
            */

            /* Creating objects for every single editorial is long running work, so we put that work on a background queue, to keep the app very responsive. */
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {


                // Making an array of all the node IDs from the JSON file
                var nodeIDArray : [Int]
                /* var nodeCounter : Int = 0 */


                if (JSON .isKindOfClass(NSDictionary)) {

                    for node in JSON as! Dictionary<String, AnyObject> {


                    var nodeIDValue = node.0
                    var lastItem : Int = 0

                    self.nodeIDArray.addObject(nodeIDValue)

                    if var editorialElement : EditorialElement = EditorialElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init") {

                editorialElement.title = node.1["title"] as! String
                editorialElement.nodeID = Int(nodeIDValue)
                let timeStampString = node.1["revision_timestamp"] as! String
                editorialElement.timeStamp = Int(timeStampString)!


                editorialElement.imageURL = String(node.1["image_url"])
                editorialElement.author = String(node.1["author"])
                editorialElement.issueNumber = String(node.1["issue_int"])
                editorialElement.volumeNumber = String(node.1["volume_int"])
                editorialElement.articleContent = String(node.1["html_content"])

                lastItem = self.editorialObjects.count

                self.editorialObjects.addObject(editorialElement)

                /* Sorting the elements in order of newest to oldest (as the array index increases] */
                self.editorialObjects.sort({ $0.timeStamp > $1.timeStamp })

                let indexPaths = (lastItem..<self.editorialObjects.count).map { NSIndexPath(forItem: $0, inSection: 0) }

                dispatch_async(dispatch_get_main_queue()) {
                            self.editorialsTableView.reloadData()
                            //                                self.editorialsTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic) // Animation implemented for testing, to be removed for version 1.0
                        }
               /*

                        nodeIDArray[nodeCounter] = jsonValue{nodeCounter}.string
                        let editorialInfos : EditorialElement = ((jsonValue as! NSDictionary].1["\(nodeIDArray[nodeCounter]]"] as! [NSDictionary]].map { EditorialElement(title: $0["title"] as! String, nodeID: $0["nid"] as! Int, timeStamp: $0["revision_timestamp"] as! Int, imageURL: $0["image_url"] as! String, author: $0["author"], issueNumber: $0["issue_int"] as! Int, volumeNumber: $0["volume_int"] as! Int, articleContent: $0["html_content"] as! String] // I am going to try to break this line down to simplify it and fix the build errors

* /                     }

                print(self.editorialObjects.count)


            }
        }


            /* Sorting the elements in order of newest to oldest (as the array index increases] */
            self.editorialObjects.sort({ $0.timeStamp > $1.timeStamp })


               let lastItem = self.editorialObjects.count

                let indexPaths = (lastItem..<self.editorialObjects.count).map { NSIndexPath(forItem: $0, inSection: $0) }

            dispatch_async(dispatch_get_main_queue()) {
                   self.editorialsTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic) // Animation implemented for testing, to be removed for version 1.0
                }

                self.currentPage++
            } */
        }

        self.populatingEditorials = false

    }

}

}

为什么我尝试加载第二组10个对象后我的应用程序崩溃了?

编辑1:cellForRowAtIndexPath代码

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let row = indexPath.row
    /*
    cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline]
    cell.editorialHeadlineLabel.text = editorialHeadline[row]

    cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline]
    cell.editorialAuthorLabel.text = editorialAuthor[row]

    cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline]
    cell.editorialPublishDateLabel.text = editorialPublishDate[row]
    */

    let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell

    let title = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).title
    let timeStamp = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp
    let imageURL = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).imageURL
    let author = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).author

    let issueNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).issueNumber
    let volumeNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).volumeNumber

    let articleContent = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).articleContent

    /* Unlike the Pictures Collection View, there is no need to create another Alamofire request here, since we already have all the content we want from the JSON we downloaded. There is no URL that we wish to place a request to to get extra content. */

    cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
    cell.editorialHeadlineLabel.text = title

    cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    cell.editorialAuthorLabel.text = author

    cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    cell.editorialPublishDateLabel.text = String(timeStamp)


    cell.editorialVolumeAndIssueLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    cell.editorialVolumeAndIssueLabel.text = "Volume \(volumeNumber) - Issue \(issueNumber)"

    return cell
}

编辑2:当我向下滚动视图时,editorialObjects.count的屏幕截图。

editorialObjects.count

0 个答案:

没有答案