Ios 如何在用户滚动时跟踪和记录UITableViewCell视图?

Ios 如何在用户滚动时跟踪和记录UITableViewCell视图?,ios,objective-c,uitableview,analytics,Ios,Objective C,Uitableview,Analytics,当用户在UITableView中向下滚动时,我们如何跟踪每个UITableViewCell视图?用户需要查看单元格一段时间(1秒)才能将其视为视图 下面的基本跟踪无法给出准确的结果,因为用户可能会快速滚动,并且不应将其视为单元视图 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

当用户在UITableView中向下滚动时,我们如何跟踪每个UITableViewCell视图?用户需要查看单元格一段时间(1秒)才能将其视为视图

下面的基本跟踪无法给出准确的结果,因为用户可能会快速滚动,并且不应将其视为单元视图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    [self logCellView:indexPath objectId:object.objectId];
}

然后


一种解决方案可能低于此,但是否有更稳定、更安全的方法

-(void)logCellView:(NSIndexPath *)indexPath object:(NSString *)object {
    // Set the timer to log the view since we don't want to log fast scrolls
    if (indexPath.row >= [logViewTimers count] && object) {
        [logViewTimers insertObject:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(logCellViewQuery:) userInfo:object repeats:NO] atIndex:indexPath.row];
    } else {
        // Cell is viewed again, set a longer interval
        [logViewTimers replaceObjectAtIndex:indexPath.row withObject:[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(logCellViewQuery:) userInfo:object repeats:NO]];
    }
}

-(void)logCellViewQuery:(NSTimer*)theTimer {
     NSLog (@"Will save the log for: %@", (NSString*)[theTimer userInfo]);
    // Query here (hidden for security)
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Gone indexPath: %ld",(long)indexPath.row);
    // Kill the timer to log the view since user scrolled fast
    if (indexPath.row < [logViewTimers count]) {
        if (((NSTimer*)[logViewTimers objectAtIndex:indexPath.row])) {
            [((NSTimer*)[logViewTimers objectAtIndex:indexPath.row]) invalidate];
        }
    }
}
-(void)logCellView:(NSIndexPath*)indexPath对象:(NSString*)对象{
//设置计时器以记录视图,因为我们不想记录快速滚动
if(indexath.row>=[logViewTimers计数]&&object){
[logViewTimers insertObject:[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(logCellViewQuery:)用户信息:对象重复:否]atIndex:indexath.row];
}否则{
//再次查看单元格时,请设置更长的间隔
[logViewTimers replaceObjectAtIndex:indexPath.row withObject:[NSTimer scheduledTimerWithTimeInterval:3目标:自选择器:@selector(logCellViewQuery:)用户信息:对象重复:否]];
}
}
-(void)logCellViewQuery:(NSTimer*)计时器{
NSLog(@“将为以下内容保存日志:%@,(NSString*)[theTimer userInfo]);
//此处查询(为安全起见隐藏)
}
-(void)tableView:(UITableView*)tableView DiEndDisplayingCell:(UITableViewCell*)RowatineXpath的单元格:(NSIndexPath*)indexPath{
NSLog(@“Gone indexath:%ld”,(long)indexath.row);
//关闭计时器以记录视图,因为用户滚动速度很快
if(indexPath.row<[logViewTimers计数]){
if(((NSTimer*)[logViewTimers对象索引:indexPath.row])){
[((NSTimer*)[logViewTimers对象索引:indexath.row])无效];
}
}
}

下面可能有一种解决方案,但是否有更稳定、更安全的方法

-(void)logCellView:(NSIndexPath *)indexPath object:(NSString *)object {
    // Set the timer to log the view since we don't want to log fast scrolls
    if (indexPath.row >= [logViewTimers count] && object) {
        [logViewTimers insertObject:[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(logCellViewQuery:) userInfo:object repeats:NO] atIndex:indexPath.row];
    } else {
        // Cell is viewed again, set a longer interval
        [logViewTimers replaceObjectAtIndex:indexPath.row withObject:[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(logCellViewQuery:) userInfo:object repeats:NO]];
    }
}

-(void)logCellViewQuery:(NSTimer*)theTimer {
     NSLog (@"Will save the log for: %@", (NSString*)[theTimer userInfo]);
    // Query here (hidden for security)
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Gone indexPath: %ld",(long)indexPath.row);
    // Kill the timer to log the view since user scrolled fast
    if (indexPath.row < [logViewTimers count]) {
        if (((NSTimer*)[logViewTimers objectAtIndex:indexPath.row])) {
            [((NSTimer*)[logViewTimers objectAtIndex:indexPath.row]) invalidate];
        }
    }
}
-(void)logCellView:(NSIndexPath*)indexPath对象:(NSString*)对象{
//设置计时器以记录视图,因为我们不想记录快速滚动
if(indexath.row>=[logViewTimers计数]&&object){
[logViewTimers insertObject:[NSTimer scheduledTimerWithTimeInterval:1目标:自选择器:@selector(logCellViewQuery:)用户信息:对象重复:否]atIndex:indexath.row];
}否则{
//再次查看单元格时,请设置更长的间隔
[logViewTimers replaceObjectAtIndex:indexPath.row withObject:[NSTimer scheduledTimerWithTimeInterval:3目标:自选择器:@selector(logCellViewQuery:)用户信息:对象重复:否]];
}
}
-(void)logCellViewQuery:(NSTimer*)计时器{
NSLog(@“将为以下内容保存日志:%@,(NSString*)[theTimer userInfo]);
//此处查询(为安全起见隐藏)
}
-(void)tableView:(UITableView*)tableView DiEndDisplayingCell:(UITableViewCell*)RowatineXpath的单元格:(NSIndexPath*)indexPath{
NSLog(@“Gone indexath:%ld”,(long)indexath.row);
//关闭计时器以记录视图,因为用户滚动速度很快
if(indexPath.row<[logViewTimers计数]){
if(((NSTimer*)[logViewTimers对象索引:indexPath.row])){
[((NSTimer*)[logViewTimers对象索引:indexath.row])无效];
}
}
}
首先是自定义单元格:

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property (assign, nonatomic) NSTimeInterval time;

@end
首先是自定义单元格:

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

@property (assign, nonatomic) NSTimeInterval time;

@end

尝试使用willDisplayCell:method-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)单元格for rowatindexpath:(nsindepath*)indexpath谢谢@bhushanparkar。方法willDisplayCell与cellForRowAtIndexPath类似,因为该操作在查看单元格时立即触发。如果单元格被查看了一段时间,我将尝试实现日志记录。请尝试使用willDisplayCell:method-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell for Rowatindexpath:(NSIndexPath*)InExpathThank@bhushanparkar。方法willDisplayCell与cellForRowAtIndexPath类似,因为该操作在查看单元格时立即触发。我试图实现记录,如果细胞是查看了一段时间。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *myCell = (MyCell *)cell;
    myCell.time = [[NSDate date] timeIntervalSince1970];
}

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *myCell = (MyCell *)cell;
    NSTimeInterval startTime = myCell.time;
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime - startTime >= 1) {
        NSLog(@"Logging.. %i.Row Display Time = %f", indexPath.row, currentTime-startTime);
    }
}