Deriving UITableView sections from NSFetchedResultsController using “to many” relationship

允我心安 提交于 2020-01-13 02:14:30

问题


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.


回答1:


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.




回答2:


I'd be tempted to ditch NSFetchResultsController because I don't see much benefit here but I've not put too much thought into it so I could be wrong.

What you could do is:

  1. Perform a fetch request for all category's and place them into an NSArray. These will be your sections.
  2. For section count return the count of category's
  3. For row count return the count of self.category.articles

Here's some example code for step 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];
}


来源:https://stackoverflow.com/questions/8335382/deriving-uitableview-sections-from-nsfetchedresultscontroller-using-to-many-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!