如何在 "tableview" 的不同部分显示来自 "coredata" 的多个实体?

How to display in separate sections of "tableview" several entities from "coredata"?

核心数据中有两个不相关的实体。我想在单个 UITableView 的两个部分中显示这些实体的示例(以及能够从 UITableView 中添加或删除它们)。

我如何使用一个 UIFetchedResultsController 来实现这个任务,是否可以只用一个 fetched results controller 来完成?

更新

使用 2 个 fetchedResultsControllers,但出现错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (1) beyond bounds (1)'

这是我的实现代码:

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        Education* education = [self.fetchedResultsController1 objectAtIndexPath:indexPath];
        cell.textLabel.text = education.educationType;
    } else {
        FamilyStatus* familyStatus = [self.fetchedResultsController2 objectAtIndexPath:indexPath];
        cell.textLabel.text = familyStatus.familyStatusType;
    }
}

#pragma mark - UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 1) {

        NSInteger rowsCount;
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController1 sections] objectAtIndex:0];

        if (self.isEdit) {

            rowsCount = [sectionInfo numberOfObjects] + 1;

        } else {

            rowsCount = [sectionInfo numberOfObjects];
        }

        return rowsCount;

    } else {

        NSInteger rowsCount;
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController2 sections] objectAtIndex:0];

        if (self.isEdit) {

            rowsCount = [sectionInfo numberOfObjects] + 1;

        } else {

            rowsCount = [sectionInfo numberOfObjects];
        }

        return rowsCount;

    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* cellIdentifier = @"Cell";
    static NSString* addCellIdentifier = @"CellAddButton";

    if (indexPath.section == 0) {

        if (indexPath.row == 0 && self.isEdit) {

            AddViewCell* cell = [tableView dequeueReusableCellWithIdentifier:addCellIdentifier];

            if (!cell) {
                cell = [[AddViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addCellIdentifier];
            }

            return cell;
        }

        else {

            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }

            if (self.isEdit) {

                NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
                [self configureCell:cell atIndexPath:path];

            } else {
                [self configureCell:cell atIndexPath:indexPath];
            }

            return cell;
        }

    } else if (indexPath.section == 1) {

        if (indexPath.row == 0 && self.isEdit) {

            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"aaa"];

            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaa"];
            }

            return cell;
        }

        else {

            UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"bbb"];

            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bbb"];
            }

            if (self.isEdit) {

                NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
                [self configureCell:cell atIndexPath:path];

            } else {

                [self configureCell:cell atIndexPath:indexPath];
            }

            return cell;
        }
    }

    return nil;
}

您不能使用一个 FRC,因为它与单个实体相关联。但是,您可以使用一组 FRC,每个实体一个,每个部分一个。您需要对索引路径进行一些操作,因为每个 FRC 都希望为 table.

的第 0 节提供数据