我有两个实体,WMDGCategory
和 WMDGActivity
。他们的关系配置如下:
我希望我的 tableview 显示根据相关联的 WMDGCategory.name
在部分标题下分组的 WMDGActivity
。
在使用单个 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
我欠你们和这里的其他人一份真正的感激之情。谢谢!
最佳答案
由于每个事件都将引用它所属的类别,因此您只需(针对事件)发出一个请求,然后您就可以考虑按它们各自的类别属性对它们进行分组。
关于ios - 实体和关系 : How to configure my tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22209702/