Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新排列NSFetchedResultController按日期排序的节_Ios_Date_Swift_Nsfetchedresultscontroller_Sections - Fatal编程技术网

Ios 重新排列NSFetchedResultController按日期排序的节

Ios 重新排列NSFetchedResultController按日期排序的节,ios,date,swift,nsfetchedresultscontroller,sections,Ios,Date,Swift,Nsfetchedresultscontroller,Sections,我对NSFetchedResultController有问题。我有一个按钮,它将两个值保存到核心数据:actualDate和shortDate。actualDate是一个NSDate变量(例如2014-12-04 08:35:59+0000),shortDate是一个字符串,通过NSDateFormatter(例如04 dec)从actualDate创建 该按钮下方是一个表格视图,应显示所有条目,按日期降序排序,并以shortDate作为每天的节名(例如,04 dec)。运行应用程序时,这与我当

我对NSFetchedResultController有问题。我有一个按钮,它将两个值保存到核心数据:actualDate和shortDate。actualDate是一个NSDate变量(例如2014-12-04 08:35:59+0000),shortDate是一个字符串,通过NSDateFormatter(例如04 dec)从actualDate创建

该按钮下方是一个表格视图,应显示所有条目,按日期降序排序,并以shortDate作为每天的节名(例如,04 dec)。运行应用程序时,这与我当前的代码配合良好

当我关闭应用程序并再次启动时,问题就会出现。条目仍按正确的顺序排列,但节名已重新排列,例如,11月30日显示在12月4日之前。我真的不知道问题是什么,也不知道如何解决

感谢您的帮助

以下是我的NSFetchedResultController代码:

// MARK: - Fetched results controller

var fetchedResultsController: NSFetchedResultsController {
    if _fetchedResultsController != nil {

        return _fetchedResultsController!
    }

    let fetchRequest = NSFetchRequest()
    // Edit the entity name as appropriate.
    let entity = NSEntityDescription.entityForName("List", inManagedObjectContext: self.managedObjectContext!)
    fetchRequest.entity = entity

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "actualDate", ascending: false)
    let sortDescriptor2 = NSSortDescriptor(key: "shortDate", ascending: false)

    fetchRequest.sortDescriptors = [sortDescriptor, sortDescriptor2]


    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "shortDate", cacheName: nil)
    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    var error: NSError? = nil
    if !_fetchedResultsController!.performFetch(&error) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        //println("Unresolved error \(error), \(error.userInfo)")
        abort()
    }

    return _fetchedResultsController!
}
var _fetchedResultsController: NSFetchedResultsController? = nil

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
    switch type {
    case .Insert:
        self.tableView.insertSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
    case .Delete:
        self.tableView.deleteSections(NSIndexSet(index: sectionIndex), withRowAnimation: .Fade)
    default:
        return
    }
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
 /*   case .Update:
        self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)*/
    case .Move:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    default:
        return
    }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    self.tableView.endUpdates()
}

我知道这是一个老问题,但我也遇到了同样的问题,我设法通过设置
cacheName
而不是让它
nil
来解决这个问题


我为每个面临同样问题的人回答了这个老问题。

您的日期是如何存储在核心数据中的?您尝试过这个吗?这个问题有解决办法吗?上面的链接提供了按日期显示sectionKey对象的方法,但不要涉及当应用程序恢复时,部分标题现在显示不正常的问题。