Basic data fetchedresultscontroller question: what are the sections for?

I am trying to find out Core Data (I am new to iPhone development), and for this I am trying to get an object from fetchedresultscontroller and NSlog by its name (object property). I tried to do it like this:

NSArray *ar = [NSArray arrayWithArray:[fetchedResultsController sections]];
Task *t = [ar objectAtIndex:0];
NSLog(@"%@", t.taskName);

However, the application crashed with this error: the application crashes with an error

Application termination due to a non-displayed exception "NSInvalidArgumentException", reason: '*** - [_ NSDefaultSectionInfo taskName]: an unrecognized selector was sent, for example 0x3d1f670'

Since then, I found out that for this you need to use the property of the selected objects, but what are the sections for? Thanks for any help, sorry if this is a very stupid question. I looked through the documentation, but still do not understand.

+5
source share
1 answer

NSFetchedResultsControllercan group your objects into sections based on the key you specify. For example, if your organization had a property category, you could group your objects using categoryand display them under the heading for this category.

Each UITableView(and each NSFetchedResultsController) contains at least one partition, so even if you do not use this function, you still have to deal with partitions. If you check the documentation , you will see that the objects in the property sectionsmatch NSFetchedResultsSectionInfo, which has a property objectsthat contains the objects in this section.

NSFetchedResultsController:

id <NSFetchedResultsSectionInfo> sectionInfo = [fetchedResultsController.sections objectAtIndex:0];
Task *task = [sectionInfo.objects objectAtIndex:0];

, - objectAtIndexPath:.

Task *task = [fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

. UIKit NSIndexPath, . , UITableViewDataSource UITableViewDelegate NSIndexPath, objectAtIndexPath:, .

Apple Core Data iPhone. . , NSFetchedResultsController.

+27

All Articles