I'm wondering if there's a (simple) way to achieve the following using a NSFetchedResultsController:
say I have a visitor log table like this:
visitor_name (String)
visit_time (Date)
visited_building_id (String)
example data:
John, 2015-09-25 10:00:00, ABC
Jane, 2015-09-25 10:20:00, ABC
Mark, 2015-09-25 10:40:00, ABC
Jane, 2015-09-25 10:10:00, DEF
And I want to show a table view which shows who most recently visited each building, so for the data above it should show two rows:
[ABC, Mark, 9/25 10:40]
[DEF, Jane, 9/25 10:10]
One solution would be to:
visited_building_id
(ascending or descending as you prefer) and then by visit_time
(descending). visited_building_id
as the sectionNameKeyPath
for the fetched results controller. The FRC will therefore have a section for each building, and the first object in each section will be the most recent visit. You want your table view rows to display the first item in each section of the FRC. So... Amend the tablewView datasource methods to remap the FRC sections to your tableView's rows. So for example numberOfRowsInSection
would return frc.sections.count
. cellForRowAtIndexPath
would construct a new NSIndexPath
using
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:(indexPath.row)]
or
let newIndexPath = NSIndexPath(forRow:0, inSection:indexPath.row)
and uses this newIndexPath
to obtain the object to display in the cell using the frc's objectAtIndexPath
method.
Note that this remapping must be repeated/reversed in the other datasource methods. And it also means your tableView can have only one section.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.