As each activity will have a reference to the category it belongs to, you would only need to make the one request (for the activities) and then you could look at grouping them by their respective category properties.
Entities and relationships: How to configure my tableview?
-
09-06-2023 - |
Pergunta
I have two entities, WMDGCategory
and WMDGActivity
. Their relationships are configured like this:
I want my tableview to display the WMDGActivity
s grouped under sections title according to their associated WMDGCategory.name
.
After failing miserably to achieve this with a single NSFetchedResultsController
(and despite some posts claiming it can be done), I've decided to try two separate FRCs, catFRC
and actFRC
. These are currently declared like this:
- (void)viewDidLoad
{
[super viewDidLoad];
catFRC = [WMDGCategory MR_fetchAllSortedBy:@"name" ascending:(YES) withPredicate:nil groupBy:@"name" delegate:nil];
actFRC = [WMDGActivity MR_fetchAllSortedBy:@"name" ascending:YES withPredicate:nil groupBy:@"catFRC.activities" delegate:nil];
[self.tableView reloadData];
}
At this point my tableviewDataSource methods look like this. Pseudo code marked with this ----> shows the current state of my confusion.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [[catFRC sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
id<NSFetchedResultsSectionInfo> sectionInfo = [[catFRC sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
WMDGActivity *activityForCell = // ----> the activities pointed to by the "WMDGCategory" relationship "activities", sorted by "name"
cell.textLabel.text = activityForCell.name;
return cell;
}
Can someone please tell me if I'm on the right track or not, and if I am please give me some guidance as to what exactly should constitute the argument for WMDGActivity *activityForCell =
.
Thanks very much for your time!
Edit following suggestions from mMo and Wain:
Thanks to your on the point suggestions, I modified my code to use just the one FRC, actFRC
, and it appears to work perfectly:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [[actFRC sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
id<NSFetchedResultsSectionInfo> sectionInfo = [[actFRC sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionLabel = [[[actFRC sections] objectAtIndex:section]name];
return [sectionLabel uppercaseString];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
WMDGActivity *thisActivity = [actFRC objectAtIndexPath:indexPath];
cell.textLabel.text = thisActivity.name;
return cell;
}
My problem throughout this, in retrospect, stemmed from my difficulty in understanding that a call to WMDGActivity.category
is a call to the WMDGCategory
object associated with the WMDGActivity
through the relationship. I know that must sound pretty stupid, but I had a great deal of trouble getting my head around the concept.
I owe you guys, and others here, a real debt of gratitude. Thanks!
Solução