我有两个实体WMDGCategory
和WMDGActivity
。他们的关系配置如下:
我希望我的tableview根据相关的WMDGActivity
显示分组在标题下的WMDGCategory.name
。
在单一NSFetchedResultsController
(尽管有些帖子声称可以完成)之后惨遭失败之后,我决定尝试两个单独的FRC,catFRC
和actFRC
。目前这些声明如下:
- (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];
}
此时我的tableviewDataSource方法如下所示。用这个标记的伪代码---->显示了我混乱的现状。
#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;
}
有人可以告诉我,如果我是在正确的轨道上,如果我是,请给我一些指导,说明究竟应该构成WMDGActivity *activityForCell =
的论据。
非常感谢您的时间!
修改以下来自mMo和Wain的建议:
感谢您提出的建议,我修改了我的代码,只使用了一个FRC,actFRC
,它似乎完美无缺:
#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;
}
回想起来,我的问题源于我很难理解调用WMDGActivity.category
是通过关系调用与WMDGCategory
相关联的WMDGActivity
对象。我知道这听起来非常愚蠢,但是我很难理解这个概念。
我欠你们这些人以及其他人的感激之情。谢谢!
答案 0 :(得分:2)
由于每个活动都会引用它所属的类别,因此您只需要发出一个请求(对于活动),然后您可以查看按类别属性对它们进行分组。