I have a UITableView which presents elements which were received as xml and then sorted out into sections and rows. I track which elements were displayed on screen using indexPathsForVisibleRows and hold it into NSMutableSet:
@interface MainFeedTableViewController () <UIGestureRecognizerDelegate>
{
NSMutableSet *viewedCellsIndexPaths;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
...
viewedCellsIndexPaths = [[NSMutableSet alloc] init];
...
}
- (void) trackVisibleCells {
// create array of currently displayed cells
NSArray *visibleCells = [_mainFeed indexPathsForVisibleRows];
// add cells to viewedCells set
[viewedCellsIndexPaths addObjectsFromArray:visibleCells];
// get unviewed cells count
NSUInteger cellsCount = 0;
NSInteger sections = [_mainFeed numberOfSections];
for (int i = 0; i < sections; i++) {
cellsCount += [_mainFeed numberOfRowsInSection:i];
}
NSUInteger unViewedCellsCount = cellsCount - [viewedCellsIndexPaths count];
// pass unviewed cells count to cell
if (self.catchupCellDelegate) {
[self.catchupCellDelegate updateUnreadCellsCount:unViewedCellsCount];
}
and now I want to open a new view that will display only the items that were not yet displayed.
Based on this answer I tried to create another set from the data source and then [allData minusSet:viewedCellsIndexPaths]
. Problem is viewedCellsIndexPaths holds IndexPaths, and the data source holds, well, data.
I've tried adding the whole tables' indexPaths to another set inside CellForRowAtIndexPath but that gave me only the visible ones and not the entire table's index paths.
Then I tried to use NSMutableOrderedSet to keep track of the cells that were visible and then call its LastObject to find out which was the last viewed cell and access the data source from that index:
NSIndexPath *lastIndex = (NSIndexPath *)viewedCellsIndexPaths.lastObject;
but I got an exception [__NSSetM lastObject]: unrecognized selector sent to instance 0x2837acb2.
sounds like a bug within the language which I can't figure a way to work around.
My final attempt based on this answer was:
NSMutableArray *unviewedCellsData = [NSMutableArray arrayWithArray:sectionOrderedFeed];
for (NSIndexPath *indexPath in viewedCellsIndexPaths) {
Items *item = [[sectionOrderedFeed objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[unviewedCellsData removeObject:item];
}
But after iteration unViewedCellsData is exactly the same as sectionOrderedFeed.
Any help here?