There are two unrelated entities in Core Data. I would like to display samples of these entities in two sections in a single UITableView
(as well as to be able to add or remove them from UITableView
).
How can I implement this task Using one UIFetchedResultsController
is it even possible to do it with only one fetched results controller ?
UPDATE
Use 2 fetchedResultsControllers, but i have error:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (1) beyond bounds (1)'
This is my implementation code:
-(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;
}
You can't use one FRC because it is tied to a single entity. But, you can use a set of FRCs, one per entity, and one per section. You would need to do some manipulation of the index paths because each FRC will expect to be supplying data for section 0 of the table.