• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS tableView 曝光埋点去重逻辑

武飞扬头像
刘小哈哈哈
帮助1

思路

记录上次曝光时展示的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]];
    }
}

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcbjai
系列文章
更多 icon
同类精品
更多 icon
继续加载