检索未查看的 UITableViewCells 的数据源
Posted
技术标签:
【中文标题】检索未查看的 UITableViewCells 的数据源【英文标题】:Retrieving Data Source of Unviewed UITableViewCells 【发布时间】:2020-04-30 12:59:19 【问题描述】:我有一个 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];
现在我想打开一个只显示尚未显示的项目的新视图。
基于this answer,我尝试从数据源创建另一个集合,然后[allData minusSet:viewedCellsIndexPaths]
。问题是viewedCellsIndexPaths持有IndexPaths,而数据源持有,嗯,数据。
我尝试将整个表的 indexPaths 添加到 CellForRowAtIndexPath 内的另一个集合中,但这只给了我可见的索引路径,而不是整个表的索引路径。
然后我尝试使用 NSMutableOrderedSet 来跟踪可见的单元格,然后调用它的 LastObject 来找出最后查看的单元格并从该索引访问数据源:
NSIndexPath *lastIndex = (NSIndexPath *)viewedCellsIndexPaths.lastObject;
但是我得到了一个异常 [__NSSetM lastObject]: unrecognized selector sent to instance 0x2837acb2。
听起来像a bug within the language,我想不出办法解决。
我最后一次尝试based on this answer 是:
NSMutableArray *unviewedCellsData = [NSMutableArray arrayWithArray:sectionOrderedFeed];
for (NSIndexPath *indexPath in viewedCellsIndexPaths)
Items *item = [[sectionOrderedFeed objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[unviewedCellsData removeObject:item];
但迭代后 unViewedCellsData 与 sectionOrderedFeed 完全相同。
这里有什么帮助吗?
【问题讨论】:
你在哪里打电话trackVisibleCells
?
测试你的代码,我得到Property 'lastObject' not found on object of type 'NSMutableSet *'
,在NSIndexPath *lastIndex = (NSIndexPath *)viewedCellsIndexPaths.lastObject;
这是正常的(这就是崩溃的意思)。
@Larme,正如我上面所说,在尝试这条线之前,我尝试将viewedCellsIndexPaths
类型更改为NSOrderedMutableSet
,您可能已经错过了它。 @DonMag trackVisibleCells
在表格完成加载后立即调用
【参考方案1】:
这是我为快速测试所做的:
@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(@"");
【讨论】:
以上是关于检索未查看的 UITableViewCells 的数据源的主要内容,如果未能解决你的问题,请参考以下文章
Swift:如何防止未显示的 UITableViewCells 在滚动时被关闭
使用字典中的“值”填充 UITableViewCells 的问题
从 Firebase 提取的数据中没有出现 UITableViewCells