其他分享
首页 > 其他分享> > iOS tableView 曝光埋点去重逻辑

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]];
    }
}

标签:tableView,iOS,id,cell,cellArray,listBoArray,埋点,NSMutableArray
来源: https://blog.csdn.net/LIUXIAOXIAOBO/article/details/121871838