Reputation: 13
I work on an iOS app that uses Core Data and I want to display an entity using UITableView.
For that I'm using a NSFetchedResultsController. The thing I am trying to achieve is to display each record in it's section. Basically I want 1 row per section.
The methods that I am struggling with are :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
because I want to have this piece of code when computing the numberOfSectionsInTableView:
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
and I want to have return 1
when computing the numberOfRowsInSection (and obviously I can't pass indexPath.section
to numberOfSectionsInTableView
).
What is the approach in this problem/situation? Thank you.
Upvotes: 0
Views: 279
Reputation: 3456
well,
Obviously, you will have as much section as you have object :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return self.fetchedResultController.fetchedObjects.count;
}
then, you will only have one row / section since this is what you want
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
then in the cellForRowAtIndexPath use the indexPath.section as a rowIndex and the indexPath.row as the sectionIndex.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath * correctedIP = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
id myItem = [self.fetchedResultController objectAtIndexPath: correctedIP];
/* ... */
}
Upvotes: 1