简体   繁体   English

尝试在表视图中按日期对Core Data对象进行排序

[英]Trying to sort Core Data objects by date in table view

I've got a Core Data setup where each object has an NSDate, and a method that returns the date as a string (ie, "12/15/2009"). 我有一个Core Data设置,其中每个对象都有一个NSDate,以及一个将日期作为字符串返回的方法(即“ 12/15/2009”)。 The objects are displayed in a table view, and I'd like to sort them into sections based on the date string. 对象显示在表格视图中,我想根据日期字符串将它们分为几部分。 Apple's example code in the CoreDataBooks app uses this line: Apple在CoreDataBooks应用程序中的示例代码使用以下行:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];

So I altered it and used this line in my project: 因此,我对其进行了更改并在我的项目中使用了这一行:

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"workoutDateString" cacheName:@"Root"];

Where "workoutDateString" is the method that returns a workout object's date as a string. 其中“ workoutDateString”是将锻炼对象的日期作为字符串返回的方法。 My core data setup works perfectly when I leave sectionNameKeyPath as nil, and I also know that my workoutDateString method works correctly. 当我将sectionNameKeyPath保留为nil时,我的核心数据设置可以完美运行,并且我还知道我的executionDateString方法可以正常工作。 But when I set sectionNameKeyPath to workoutDateString, not only does it not display the section titles, but every time I add a workout object, the program crashes with this error: 但是,当我将sectionNameKeyPath设置为executionDateString时,它不仅不显示节标题,而且每次添加锻炼对象时,程序都会因以下错误而崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).' 由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第1节中的行数无效。更新(3)之后现有节中包含的行数必须等于该行中包含的行数更新(1)之前的部分,加上或减去从该部分插入或删除的行数(已插入0,已删除0)。”

This is my first real iPhone project and I don't have any experience with setting up table sections. 这是我第一个真正的iPhone项目,并且我没有设置表格部分的经验。 And trust me, I have spent quite a bit of time on Google looking for solutions. 相信我,我在Google上花费了很多时间来寻找解决方案。

I had this exact same problem, and spent an enormous amount of time researching it. 我遇到了完全相同的问题,并花费了大量时间进行研究。

The NSFetchedResultsControllerDelegate 'animate changes' code causes serious problems when items are added to a completely new section or all the items from a section are moved to another, leaving the original section with 0 items. NSFetchedResultsControllerDelegate的“动画更改”代码在将项目添加到一个全新的节中或将某个节中的所有项目都移到另一个节而使原始节中有0个项目时引起严重的问题。

I would either remove all the code that does fancy insertion/deletion of sections/rows, or use Jeff LaMarche's code to fix it. 我要么删除所有执行段/行插入/删除操作的代码,要么使用Jeff LaMarche的代码对其进行修复。 http://iphonedevelopment.blogspot.com/2009/11/one-fix-to-nsfetchedresultscontroller.html http://iphonedevelopment.blogspot.com/2009/11/one-fix-to-nsfetchedresultscontroller.html

EDIT: You implemented [tableView:titleForHeaderInSection], right? 编辑:您实现了[tableView:titleForHeaderInSection],对吗?

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    id <NSFetchedResultsSectionInfo> sectionInfo = [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo name];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在表视图中删除核心数据对象 - Deleting core data objects in a table view 核心数据,托管对象和多个表视图控制器 - Core data, managed objects and multiple table view controllers 核心数据:表格视图获取对象的FRC数量为零 - Core data: table view FRC number of fetched objects is zero 在表格视图部分中排序核心数据对象 - Ordering core data objects inside a table view section 在核心数据中使用日期属性在表视图控制器中创建节标题 - Creating Section Titles in a Table View Controller with date attribute in Core Data 在Table View中对核心数据进行汇总操作,并按日期进行计数和分组 - Aggregate Operation on Core Data in Table View with count and grouping by date 核心数据:从3个实体中提取对象到一个实体中并按日期排序? - Core-Data: Fetch objects from 3 entities into one fetch and sort by date? 如何按修改日期对核心数据对象进行排序并在 tableView 中以正确的顺序列出? - How do I sort core data objects by date modified and list it in the right order in a tableView? 如何对两个不同的核心数据对象进行排序? - How to sort two different core data objects? 使用Core Data时按日期对TableView进行排序 - Sort TableView by date while using Core Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM