iOS tableView 曝光埋点去重逻辑

文章目录

思路

记录上次曝光时展示的cell, 和这次展示的cell 数组对比,上次没有的cell,就是这次需要曝光的cell

代码

//self.preTempArray 记录了上次曝光时候的数据,用来和这次的对比,做数据去重
- (void)cardExposureStatistics:(UITableView *)tableView dataSource:(NSMutableArray *)dataArray cellHeight:(CGFloat)cellHeight superView:(UIView *)superView pv_id:(NSString *)pv_id {
    if (dataArray.count == 0) {
        return;
    }
    NSMutableArray *cellArray = [NSMutableArray new];
    [cellArray removeAllObjects];
    [cellArray addObjectsFromArray:tableView.visibleCells];
    
    UITableViewCell *firstCell = [cellArray firstObject];
    UITableViewCell *lastCell = [cellArray lastObject];
    
    NSIndexPath *firstIndexPath = [tableView indexPathForCell:firstCell];
    NSIndexPath *lastIndexPath = [tableView indexPathForCell:lastCell];
    
    CGRect firstRect = [tableView rectForRowAtIndexPath:firstIndexPath];
    CGRect lastRect = [tableView rectForRowAtIndexPath:lastIndexPath];
    
    CGFloat firstCellY = [tableView convertRect:firstRect toView:superView].origin.y;
    CGFloat lastCellY = [tableView convertRect:lastRect toView:superView].origin.y;
    
    if (cellHeight/2+firstCellY < SafeAreaTopHeight) {
    /// 这里是cell需要有超过一半展示在屏幕中
        [cellArray removeObject:firstCell];
    }
    
    if (cellHeight/2+lastCellY > bottomBarHeight) {
        /// 这里是cell需要有超过一半展示在屏幕中
        [cellArray removeObject:lastCell];
    }
    
    NSMutableArray *listBoArray = [NSMutableArray new];//这个记录上次屏幕出现的卡片,之后会筛掉preArray存的卡片
    NSMutableArray *tempListBoArray = [NSMutableArray new];//这个只是为了记录上次屏幕出现的卡片,给preArray赋值
    [cellArray enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL * _Nonnull stop) {
        NSIndexPath *indexPath = [tableView indexPathForCell:cell];
        id data = dataArray[indexPath.row];
        [listBoArray addObject:data];
        [tempListBoArray addObject:data];
    }];
    
    [self.preTempArray enumerateObjectsUsingBlock:^(listContObjectVO  *listItem, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([listBoArray containsObject:listItem]) {
        /// 这里是做数据去重,去掉上次展示过的数据
            [listBoArray removeObject:listItem];
        }
    }];
    [self.preTempArray removeAllObjects];
    ///这里更新数据,下次展示的时候去重
    [self.preTempArray addObjectsFromArray:tempListBoArray];
    
    if (listBoArray.count > 0) {
    /// listBoArray是已经去过重的数据
        [[LogManager shareInstance] cardExposure:[ServiceAnalysisModel getCardExposureAnalysisModelArray:listBoArray pv_id:pv_id]];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值