Ios [\uu\u NSOrderedSetM objectAtIndex:]:索引10超出范围[0..9[
我将包含要在表视图中使用的所有JSON对象的主数据结构从NSMutableArray更改为NSMutableOrderedSet。现在,我的应用程序正确地从API加载了前十个对象,但当我想向下滚动时,它崩溃了。有什么建议吗Ios [\uu\u NSOrderedSetM objectAtIndex:]:索引10超出范围[0..9[,ios,swift,Ios,Swift,我将包含要在表视图中使用的所有JSON对象的主数据结构从NSMutableArray更改为NSMutableOrderedSet。现在,我的应用程序正确地从API加载了前十个对象,但当我想向下滚动时,它崩溃了。有什么建议吗 override func scrollViewDidScroll(scrollView: UIScrollView) { if (scrollView.contentOffset.y + view.frame.size.height > scrollView.
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
编辑2:编辑对象的屏幕截图。向下滚动视图时计数
它在哪一行崩溃?(您可以使用异常断点来确定它当前是否未被报告。)它在我的“cellForIndexPath”函数中崩溃,在我使用函数“objectAtIndex”的第一行。我刚刚添加了cellForIndexPath代码。您从
numberOfRowsInSection
返回什么?(应该是editorialObjects的计数)我不小心将其更改为nodeIDArray.count,尽管我显然知道正确的是editorialObjects.count(这是我之前设置的,我只是想测试不同的值,看看会发生什么)。错误现在消失了。如果您能帮我解决另一个问题,那将是非常棒的。当我激活“scrollViewDidScroll”时方法通过向下滚动视图,editorialObjects集合中不会添加任何内容。我打印了editorialObjects.count值,但它没有超过10,即使在我使用NSMutableArray时,它确实超过了10。我添加了一个屏幕截图,以便您可以查看发生了什么。当我删除时,表视图也完全停止添加表单元格滚下来。
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
}
}
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
}