gpt4 book ai didi

ios - 如果显示 2 秒,则将显示的 UITableView 单元格存储在 NSString 中

转载 作者:行者123 更新时间:2023-12-01 19:04:48 25 4
gpt4 key购买 nike

我有一个 UITableView,里面有很多单元格。我想跟踪滚动了哪些单元格并将关联的 ID 存储在 NSUserDefaults 中。

这一切都非常好,但现在我想添加一个计时器,以防止将 ID 附加到字符串,除非它已在屏幕上显示整整两秒或更长时间。

这是我将每个新的滚动 ID 附加到 NSString 的行:

NSString *scrolledIdPre = [NSString stringWithFormat:@"-%@-", locationId];

这是我的完整方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

NSDictionary *item;

if (tableView == self.searchDisplayController.searchResultsTableView){
item = [[NSDictionary alloc] initWithDictionary:[filteredListItems
objectAtIndex:indexPath.row]];
}else {
item = [[NSDictionary alloc] initWithDictionary:[listItems
objectAtIndex:indexPath.row]];
}

if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"cell"];
}

NSString *locationId = [item objectForKey:@"location_id"];

NSString *scrolledIdPre = [NSString stringWithFormat:@"-%@-", locationId];

[scrolledIds appendString:scrolledIdPre];

[[NSUserDefaults standardUserDefaults] setObject:scrolledIds forKey:@"impressions"];
[[NSUserDefaults standardUserDefaults] synchronize];

return cell;
}

任何想法都会很棒。谢谢!

最佳答案

您可以使用延迟调度并检查单元格是否仍然可见

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

NSDictionary *item;

if (tableView == self.searchDisplayController.searchResultsTableView){
item = [[NSDictionary alloc] initWithDictionary:[filteredListItems
objectAtIndex:indexPath.row]];
}else {
item = [[NSDictionary alloc] initWithDictionary:[listItems
objectAtIndex:indexPath.row]];
}

if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"cell"];
}

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

// nil means that cell is no longer visible so it didn't meet the requirement for 2s displaying
if ([tableView cellForRowAtIndexPath:indexPath] == nil) {
return;
}

NSString *locationId = [item objectForKey:@"location_id"];

NSString *scrolledIdPre = [NSString stringWithFormat:@"-%@-", locationId];

[scrolledIds appendString:scrolledIdPre];

[[NSUserDefaults standardUserDefaults] setObject:scrolledIds forKey:@"impressions"];
[[NSUserDefaults standardUserDefaults] synchronize];
});

return cell;
}

关于ios - 如果显示 2 秒,则将显示的 UITableView 单元格存储在 NSString 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20346319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com