使用“到许多”来从NSFetchedResultsController导出UITableView节。关系 [英] Deriving UITableView sections from NSFetchedResultsController using "to many" relationship

查看:92
本文介绍了使用“到许多”来从NSFetchedResultsController导出UITableView节。关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Core Data模型如下所示:

  article< --->类别

甚至远程可以使用 NSFetchedResultsController 以产生类似这样的UITableView?

 类别1 
- 文章A
- B
- C
类别2
- A
- D
- E
- F
第3类
- 文章B
- 文章C

具体来说,我对边缘?)情况,其中每个UITableView节具有唯一的标题(例如,类别1,类别2),但相同的对象可以存在于多个部分中(例如,类别1和类别2中都存在文章A)。



我已经学习了苹果的核心数据文档,花了两天时间在这里讨论问题,但唉,没有运气,甚至找出是否可能,更不用说如何实现。感谢任何帮助或指针以前回答的问题。我确实找不到。

解决方案

是的,很容易,虽然有一百万种方法。 p>

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



在tableview中有一个section的概念,你可以选择为每个类别有一个。



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



这样的东西应该让你开始类别和文章实体都包含 title 属性):

   - NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
//返回类别数量
[[self.categoryResultsController fetchedObjects] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//返回单个类别的标题
[[self.categoryResultsController.fetchedObjects objectAtIndex:section] valueForKey:@title];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//返回类别中的文章数量
MyCategory * category = [self.categoryResultsController.fetchedObjects objectAtIndex:section];

return category.articles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取缓存单元格对象每一行是相同的,我们重复使用相同的对象反复)
static NSString * identifier = @ArticleCellIdentifier;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}

//查找类别和文章,并设置单元格的文本
MyCategory * category = [self.categoryResultsController.fetchedObjects objectAtIndex:indexPath.section];

cell.textLabel.text = [[category.articles objectAtIndex:indexPath.row] valueForKey:@title];

return cell;
}

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


My Core Data model looks like this:

article <--->> category

Is it even remotely possible to use NSFetchedResultsController to produce a UITableView that looks something like this?

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

Specifically, I'm interested in the (edge?) case where each UITableView section has a unique title (eg, "Category 1", "Category 2"), but the same object can exist in multiple sections (eg, Article A exists in both Category 1 and Category 2).

I have scoured Apple's Core Data docs and have spent two days perusing questions here, but alas, no luck even finding out whether this is possible, let alone how to implement it. Thanks for any help or pointers to a previously answered question. I certainly couldn't find it.

解决方案

Yes, it's easy, though there are a million ways to do it.

Your view controller should be the "data source" of the UITableView, and returns information about the number of rows there are and then the contents of each individual row.

There is a concept of a "section" in a tableview, you might choose to have one for each category.

For example, you could create an NSFetchedResultsController to find the categories you want to display, and use that to populate the table view sections, and then each category would have an articles many-to-many relationship, to populate the rows in each section.

Something like this should get you started (assuming your category and article entities both contain a title property):

- (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;
}

You can read the documentation on these methods to figure out how to customise it further.

这篇关于使用“到许多”来从NSFetchedResultsController导出UITableView节。关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆