UITableViews上的核心数据关系和/或NSPredicate排序

时间:2013-09-06 23:23:26

标签: iphone ios core-data nspredicate

我遇到了一个非常基本且有明显答案的问题,我已经阅读了Apple的一些文档以及这里的答案和在线教程。

我有一个显示“月”实体列表的桌面视图(应用程序使用核心数据)。它会转到另一个表格视图,我想显示该父月份中包含的所有日期的列表。因此,点击“月”实体列表中的一行最好只列出1月份的日期。

Month和Days表视图分别与常规视图控制器(不是tableview VC)相对应,它们分别添加Month条目和Day条目。

数据被建模,因此Month具有以天为目标的多对多关系(daysInMonths),而逆是一对一关系(parentMonth)。

月属性为'月'和'年'。 Days属性为'month','day'和'year'。

模型对象上的所有属性当前都是字符串(不理想,但我会在下一个练习中对此进行更改)。

我可以添加月份和日期条目。我可以删除它们。问题是,当我点击第一个表视图(月)中的一行时,它会显示每天的条目,而不会过滤。

我假设当我在Days tableview .m文件中设置获取的结果控制器时,我会使用谓词来仅过滤与给定父月匹配的天数。

此tableview中的我的获取请求位于Days对象上。在设置谓词时,我是否必须在该fetch方法中实例化Month对象?

我不想创建一个额外的Model对象,当我想要做的就是设置“parentMonth”关系以正确关联数据时,它会显示在tableview中。

我从概念上理解关系的概念,但我有点不清楚如何在代码中实际设置它们。

设置此类过滤器的最佳位置是什么?

我意识到我可能不够清楚,所以我会尽我所能回答任何其他问题。

我知道我遗漏了一些基本的东西,因为这种功能在任何地方都可以使用,但是我自己阅读并没有让人感到满意......

@implementation DaysCDTVC

- (void)setupFetchedResultsController
{    
    // 1 - Get Entity 
    NSString *entityName = @"Days";
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    // 2 - Request 
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

    // 3 - Filter 

    //request.predicate = [NSPredicate predicateWithFormat: ????????????????

    // 4 - Sort 
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor     sortDescriptorWithKey:@"month"
                                                                                   ascending:YES
                                                                                  selector:@selector(localizedCaseInsensitiveCompare:)]];    

    //5 - Fetch 
    self.fetchedResultsController = [[NSFetchedResultsController alloc]   initWithFetchRequest:request
                                                                      managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:nil
                cacheName:nil];

    [self performFetch];    
}

“performFetch”方法位于关联的类中。这与Paul Hegarty在斯坦福iTunes讲座中教授的内容基本相同。

1 个答案:

答案 0 :(得分:1)

我从我自己的一个项目转换代码。这应该可以解决您的问题。如果您需要澄清或有问题,请告诉我。

表格视图控制器中,创建一个公共属性:

@property (nonatomic, strong) NSString *month;

表格视图控制器中,将所选月份传递给表格视图控制器:

- (void)prepareForSegue
{
    //Get a pointer to the destination TVC.
    //Your final code should have a check to make sure the destination TVC is what you expect
    DaysTableViewController *destinationTVC = (DaysTableViewController*)segue.destinationViewController;

    //Pass over the month from the selected row.  I'm calling it selected month.
    //If you need help getting the selected month, let me know
    destinationTVC.month = selectedMonth
}

表格视图控制器中,使用过去的月份构建您的获取请求

- (void)setupFetchedResultsController
{
    //Entity
    NSString *entityName = @"Days";

    //Fetch Request
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

    //Sort
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"day" ascending:YES]];

    //Predicate
    request.predicate = [NSPredicate predicateWithFormat:@"ANY SELF.month = %@", self.month];

    //Fetch using the code you posted earlier
}
相关问题