我有一个UITableView,它展示的元素是以xml的形式接收的,然后被整理成部分和行。我使用indexPathsForVisibleRows跟踪哪些元素在屏幕上显示,并将其保存到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];
}
现在我想打开一个新的视图,只显示还没有显示的项目。
基于 本回答 我试着从数据源中创建另一个集合,然后 [allData minusSet:viewedCellsIndexPaths]
. 问题是 viewedCellsIndexPaths 持有 IndexPaths,而数据源持有,嗯,数据。
我试过在CellForRowAtIndexPath里面把整个表的索引路径添加到另一个集合中,但那只给我可见的索引路径,而不是整个表的索引路径。
然后我试着使用 NSMutableOrderedSet 来跟踪可见的单元格,然后调用它的 LastObject 来找出最后查看的单元格,并从该索引中访问数据源。
NSIndexPath *lastIndex = (NSIndexPath *)viewedCellsIndexPaths.lastObject;
但我得到了一个异常[__NSSetM lastObject]:未识别的选择器发送到实例0x2837acb2。
听起来像是 语病 我想不出办法来解决这个问题。
我最后的尝试 据此 是。
NSMutableArray *unviewedCellsData = [NSMutableArray arrayWithArray:sectionOrderedFeed];
for (NSIndexPath *indexPath in viewedCellsIndexPaths) {
Items *item = [[sectionOrderedFeed objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[unviewedCellsData removeObject:item];
}
但是在迭代之后,unViewedCellsData和sectionOrderedFeed完全一样。
这里有什么帮助吗?
这是我做的一个快速测试。
@interface ViewedCellsTableViewController ()
{
NSMutableArray *myData;
NSMutableSet *viewedCellsIndexPaths;
NSMutableOrderedSet *orderedAllIndexPaths;
}
@end
@implementation ViewedCellsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// just creating sample data...
// 6 sections with a few rows in each section
NSArray *rowsPerSection = @[@3, @5, @4, @8, @6, @7];
myData = [NSMutableArray new];
int iSection = 0;
for (NSNumber *nRows in rowsPerSection) {
NSMutableArray *aSec = [NSMutableArray new];
for (int iRow = 0; iRow < [nRows intValue]; iRow++) {
NSString *s = [NSString stringWithFormat:@"This is sec: %d row:%d", iSection, iRow];
[aSec addObject:s];
}
[myData addObject:aSec];
++iSection;
}
// initialize ordered set of all index paths
orderedAllIndexPaths = [NSMutableOrderedSet new];
iSection = 0;
for (NSArray *aSec in myData) {
for (int iRow = 0; iRow < [aSec count]; iRow++) {
NSIndexPath *p = [NSIndexPath indexPathForRow:iRow inSection:iSection];
[orderedAllIndexPaths addObject:p];
}
++iSection;
}
// init tracking set
viewedCellsIndexPaths = [NSMutableSet new];
}
然后,当准备好继续前进时... ...
NSMutableOrderedSet *unViewedCellsIndexPaths = [orderedAllIndexPaths mutableCopy];
[unViewedCellsIndexPaths minusSet:viewedCellsIndexPaths];
NSLog(@"\nViewed:\n%@ \n..", viewedCellsIndexPaths);
NSLog(@"");
NSLog(@"\nUn-Viewed:\n%@ \n..", unViewedCellsIndexPaths);
NSLog(@"");