iOS 评论盖楼功能实现逻辑

数据结构

请添加图片描述

展示效果

请添加图片描述

思路

我们这里使用同一级cell,即一级评论和二级评论是平级的cell,不存在嵌套关系,这样UI逻辑简单,所以解析数据的时候需要将子评论数组和一级评论数据放置到同一个数组里面。
具体实现的时候就是,将某条一级评论添加到数据源之后,立即将其子评论添加到数据源中,然后再遍历下一条一级评论,

数据解析

///获取评论数组
+ (NSMutableArray *)returnCommentListWithResponse:(NSArray *)commentList{
    
    
    NSMutableArray *dataList = [NSMutableArray array];
    __block NSMutableArray *cDataList = [NSMutableArray array];
    [commentList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
    
        commentObjectVO *comment = setJsonDicToDataModel(obj, [commentObjectVO class]);
        comment.sectionType = @"1";//代表评论层
        NSArray *childComList = obj[@"childList"];
        [cDataList addObject:comment];
        NSMutableArray *childList = [self getChildCommentListWithList:childComList SuperCommentBO:comment];
        ///将子评论插入到一级评论后面
        [dataList addObjectsFromArray:childList];
    }];
    [dataList addObject:cDataList];
    return dataList;
}

///获取子评论数组
+ (NSMutableArray *)getChildCommentListWithList:(NSArray *)childComList SuperCommentBO:(commentObjectVO *)superComBO{
    
    
    NSMutableArray *dataList = [NSMutableArray array];
    __block NSMutableArray *contList = [NSMutableArray array];
    [childComList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
    
        commentObjectVO *comment = setJsonDicToDataModel(obj, [commentObjectVO class]);
        comment.sectionType = @"2";//代表引用层
        [contList addObject:comment];
    }];
    if (!isBlankString(superComBO.nextUrl) && [superComBO.childNums intValue]>childComList.count) {
    
    
        commentObjectVO *unDisplayComments = [commentObjectVO new];
        unDisplayComments.sectionType = @"4";//未展开卡片
        unDisplayComments.childNums = superComBO.childNums;
        unDisplayComments.nextUrl = superComBO.nextUrl;
        //这里用hasMore来记录一下 没展开之前显示“共x跳评论” 展开一次后显示“展开更多评论”
        [contList addObject:unDisplayComments];
    }
    [dataList addObject:contList];
    return dataList;
}


Guess you like

Origin blog.csdn.net/LIUXIAOXIAOBO/article/details/121789900
ios