【问题标题】:How to section UITableView on Core Data relationship?如何在核心数据关系上划分 UITableView?
【发布时间】:2010-07-20 12:19:52
【问题描述】:

我有 2 个实体 - BlogEntry 和 BlogComments。

BlogEntry.cmets 是与 BlogComments 的“对多”关系

| BlogEntry     |
-----------------
| subject       |
| body          |
| comments (rel)|



| BlogComments   |
------------------
| commentText    |
| blogEntry (rel)|

现在,我有一个表格视图,我希望第一行是 BlogEntry 正文(文本),其余行是 BlogComments。 NSFetchedResultsController 可以做到这一点吗?我可以断绝这样的关系吗?如果是这样,有人能指出我正确的方向吗?

【问题讨论】:

    标签: ios iphone uitableview core-data


    【解决方案1】:

    使用分组的UITableView 以及使用sectionNameKeyPathNSPredicateNSFetchedResultsController

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"BlogComments" 
                                              inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    
    [fetchRequest setFetchBatchSize:20];
    
    // Ensure we get a single blog entry and all it's associated comments
    NSPredicate *predicate = 
                     [NSPredicate predicateWithFormat:@"blogEntryID=%@", blogEntryID];
    
    [fetchRequest setPredicate:predicate];
    
    NSString *key = @"blogEntry.subject";
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:key 
                                                                   ascending:YES];
    NSArray *sortDescriptors = @[sortDescriptor];
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    NSFetchedResultsController *aFetchedResultsController = 
             [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                 managedObjectContext:managedObjectContext
                                                   sectionNameKeyPath:keycacheName:nil];
    
    aFetchedResultsController.delegate = self;
    

    要确定节的标题,请使用 fetchedResultsController 的节属性:

    - (NSString *)tableView:(UITableView *)tableView 
                   titleForHeaderInSection:(NSInteger)section 
    {
       if ([[fetchedResultsController sections] count] > section)
       {
        id <NSFetchedResultsSectionInfo> sectionInfo = 
                         [[self.fetchedResultsController sections] objectAtIndex:section];
    
           return [sectionInfo name];
        }
        return nil;
    }
    

    确定特定部分的行数:

    - (NSInteger)tableView:(UITableView *)tableView 
                   numberOfRowsInSection:(NSInteger)section
    {
        NSInteger numberofRows = 0;
    
        if ([[fetchedResultsController sections] count] > 0)
        {
            id <NSFetchedResultsSectionInfo> sectionInfo = 
                     [[fetchedResultsController sections] objectAtIndex:section];
            numberofRows = [sectionInfo numberOfObjects];
        }
    
        return numberofRows;
    }
    

    最后,获取 tableview 中的部分数量:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        NSUInteger count = [[fetchedResultsController sections] count];
        return count; 
    }
    

    结果是一个表视图,其中 1 个部分表示博客条目,并且所有与它相关的博客 cmets 作为表视图中的行。

    【讨论】:

    • 您误解了要求。 OP 希望每个 tableview 有一个博客条目,即 tableview 将只显示一个博客条目,然后显示该博客条目的相关 cmets。您的解决方案为许多博客条目中的每一个创建一个部分,这不是 OP 想要的。
    • @TechZen 将获取的结果限制为 1 个博客条目(带有相应的 cmets)的谓词无意中被遗漏在代码中。我已经添加了它。即便如此,无论您想显示 1 个部分还是多个部分,这种方法都是完全合理的。
    【解决方案2】:

    您可能不想使用NSFetchedResultsController。它旨在轻松显示大量相同的托管对象。您有一个由两个托管对象组成的表。

    (我在这里假设每个表格将仅显示一篇带有其 cmets 的博客文章。)

    您需要做的是使用分段表。第一部分将显示博客文章的正文,第二部分将显示 cmets。如果在另一个视图中选择了帖子本身,那么您根本不需要获取。您只需了解帖子与其 cmets 之间的关系即可。将 cmets 放入一个数组中,然后根据需要对它们进行排序。

    numberOfSectionsInTableView: 方法中返回2。在numberOfRowsInSection: 中有一个switch 语句。如果该部分为零,则为博客文章返回 1。如果为两个,则返回评论数组的计数。在cellForRowAtIndexPath: 中重复切换以返回任一部分的正确单元格。

    【讨论】:

    • 经过大量试验和错误后,我认为这也是最好的方法。我把帖子留在这里征求第二意见。谢谢!
    • 完全不同意这个答案。 NSFetchedResultsController 用于处理分组数据,就像这个一样。每个博客条目都是一个节标题,每个评论都是该节中的一个项目。我将发布有关如何执行此操作的答案。
    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多