【问题标题】:Deriving UITableView sections from NSFetchedResultsController using "to many" relationship使用“对多”关系从 NSFetchedResultsController 派生 UITableView 部分
【发布时间】:2011-12-01 01:12:11
【问题描述】:

我的核心数据模型如下所示:

article <--->> category

是否可以远程使用NSFetchedResultsController 来生成类似这样的 UITableView?

Category 1
  - Article A
  - Article B
  - Article C
Category 2
  - Article A
  - Article D
  - Article E
  - Article F
Category 3
  - Article B
  - Article C

具体来说,我对(边缘?)每个 UITableView 部分都有一个唯一标题(例如,“Category 1”、“Category 2”)的情况感兴趣,但同一个对象可以存在于多个部分中(例如,文章 A 存在于第 1 类和第 2 类中)。

我已经搜索了 Apple 的 Core Data 文档,并在这里花了两天时间仔细研究问题,但是很遗憾,我什至没有找到这是否可行,更不用说如何实现它了。感谢您提供任何帮助或指向先前回答的问题。我肯定找不到。

【问题讨论】:

    标签: ios uitableview core-data one-to-many nsfetchedresultscontroller


    【解决方案1】:

    是的,这很容易,尽管有上百万种方法可以做到。

    您的视图控制器应该是UITableView 的“数据源”,并返回有关行数的信息,然后是每一行的内容。

    表格视图中有一个“部分”的概念,您可以选择为每个类别设置一个。

    例如,您可以创建一个NSFetchedResultsController 来查找要显示的类别,并使用它来填充表格视图部分,然后每个类别将具有文章多对多关系,以填充每个部分的行。

    这样的事情应该可以帮助您入门(假设您的类别和文章实体都包含title 属性):

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
       // return the number of categories
        [[self.categoryResultsController fetchedObjects] count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
      // return the title of an individual category
        [[self.categoryResultsController.fetchedObjects objectAtIndex:section] valueForKey:@"title"];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
      // return the number of articles in a category
      MyCategory *category = [self.categoryResultsController.fetchedObjects objectAtIndex:section];
    
      return category.articles.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // fetch a cached cell object (since every row is the same, we re-use the same object over and over)
        static NSString *identifier = @"ArticleCellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        }
    
        // find the category and article, and set the text of the cell
        MyCategory *category = [self.categoryResultsController.fetchedObjects objectAtIndex:indexPath.section];
    
        cell.textLabel.text = [[category.articles objectAtIndex:indexPath.row] valueForKey:@"title"];
    
        return cell;
    }
    

    您可以阅读有关这些方法的文档以了解如何进一步自定义它。

    【讨论】:

      【解决方案2】:

      我很想放弃NSFetchResultsController,因为我看不到这里有太多好处,但我没有考虑太多,所以我可能错了。

      你可以做的是:

      1. 对所有category 执行提取请求并将它们放入NSArray。这些将是您的部分。
      2. 对于部分计数,返回 category 的计数
      3. 对于行计数,返回self.category.articles 的计数

      这里是步骤 2 + 3 的一些示例代码

      // 2
      - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
      {
          return [self.categories count];
      }
      
      // 3
      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
      {
          return [[[self.categories objectAtIndex:section] articles] count];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多