ios - 实体和关系 : How to configure my tableview?

标签 ios uitableview core-data nsfetchedresultscontroller

我有两个实体,WMDGCategoryWMDGActivity。他们的关系配置如下:

enter image description here

我希望我的 tableview 显示根据相关联的 WMDGCategory.name 在部分标题下分组的 WMDGActivity

在使用单个 NSFetchedResultsController 实现这一目标失败后(尽管有些帖子声称可以完成),我决定尝试两个单独的 FRC,catFRCactFRC。这些目前是这样声明的:

- (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/

相关文章:

ios - 自定义 UITableViewCell 不更新

ios - 如何使用 NSFetchedResultsController 建立 A-Z 索引

ios - MagicalRecord MR_createInContext : returns nil

ios - 来自 UNNotificationSound 的声音名称

ios - App Store Connect消息:您的帐户很快将需要迁移到联合身份验证

ios - 从 SCNPhysicsBallSocketJoint 中移除弹性

iphone - UITableViewscrollToRowAtIndexPath

ios - 在后台使用照片更新 Tableview

objective-c - 将图像存储在 sqlite 中还是只是对它的引用?

ios - UITableView 分隔单元格?