iOS:二叉树多级表格的使用,使用三方库TreeTableView-master实现对json解析数据的递归遍历整理成树状结构

本文涉及的产品
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介:

在项目中,我们有时需要使用二叉树来实现多级表格的递归遍历查询,如果对二叉树比较懂,那么写起来其实也不费事,为了节省开发时间,下面介绍一下第三方库TreeTableView-master,这个三方库上给了一些静态的数据展示可供参考。

然而,在实际项目中,数据返回的很多都是json数据,需要自己递归遍历整理后,再将数据传递给TreeTableView作为数据源来实现多级列表。

Github下载地址:https://github.com/yixiangboy/TreeTableView

静态数据测试:

复制代码
//----------------------------------中国的省地市关系图3,2,1--------------------------------------------
Node *country1 = [[Node alloc] initWithParentId:-1 nodeId:0 name:@"中国" depth:0 expand:YES];
Node *province1 = [[Node alloc] initWithParentId:0 nodeId:1 name:@"江苏" depth:1 expand:NO];
Node *city1 = [[Node alloc] initWithParentId:1 nodeId:2 name:@"南通" depth:2 expand:NO];
Node *city2 = [[Node alloc] initWithParentId:1 nodeId:3 name:@"南京" depth:2 expand:NO];
Node *city3 = [[Node alloc] initWithParentId:1 nodeId:4 name:@"苏州" depth:2 expand:NO];
Node *province2 = [[Node alloc] initWithParentId:0 nodeId:5 name:@"广东" depth:1 expand:NO];
Node *city4 = [[Node alloc] initWithParentId:5 nodeId:6 name:@"深圳" depth:2 expand:NO];
Node *city5 = [[Node alloc] initWithParentId:5 nodeId:7 name:@"广州" depth:2 expand:NO];
Node *province3 = [[Node alloc] initWithParentId:0 nodeId:8 name:@"浙江" depth:1 expand:NO];
Node *city6 = [[Node alloc] initWithParentId:8 nodeId:9 name:@"杭州" depth:2 expand:NO];
//----------------------------------美国的省地市关系图0,1,2--------------------------------------------
Node *country2 = [[Node alloc] initWithParentId:-1 nodeId:10 name:@"美国" depth:0 expand:YES];
Node *province4 = [[Node alloc] initWithParentId:10 nodeId:11 name:@"纽约州" depth:1 expand:NO];
Node *province5 = [[Node alloc] initWithParentId:10 nodeId:12 name:@"德州" depth:1 expand:NO];
Node *city7 = [[Node alloc] initWithParentId:12 nodeId:13 name:@"休斯顿" depth:2 expand:NO];
Node *province6 = [[Node alloc] initWithParentId:10 nodeId:14 name:@"加州" depth:1 expand:NO];
Node *city8 = [[Node alloc] initWithParentId:14 nodeId:15 name:@"洛杉矶" depth:2 expand:NO];
Node *city9 = [[Node alloc] initWithParentId:14 nodeId:16 name:@"旧金山" depth:2 expand:NO];

//----------------------------------日本的省地市关系图0,1,2--------------------------------------------
Node *country3 = [[Node alloc] initWithParentId:-1 nodeId:17 name:@"日本" depth:0 expand:YES];
NSArray *data = [NSArray arrayWithObjects:country1,province1,city1,city2,city3,province2,city4,city5,province3,city6,country2,province4,province5,city7,province6,city8,city9,country3, nil];

  TreeTableView *tableview = [[TreeTableView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-20) withData:data];

  tableview.treeTableCellDelegate = self;

 [self.view addSubview:tableview];

复制代码

结果截图显示:

 

Github上介绍的比较详细,可以自己去看一下。

 下面我来对他进行灵活的扩展,对json数据解析递归整理后,使用它利用二叉树的特点实现多级表格显示:

后台返回的json数据格式如下,每一个知识点都有自己的属性,son是用做判断是否有子知识点的条件,son1是子知识点,pid是当前知识点的父节点,knowid是当前知识点的节点,depth是当前知识点的深度等等:

复制代码
2016-06-22 12:04:28.622 Point[1163:71939] {
    data =     (
                {
            id = 3067;
            knowid = 6582;
            name = "\U6570\U4e0e\U5f0f";
            pid = 13;
            pidA = "0-2-13";
            qNum = 123462;
            son = 1;
            son1 =             (
                                {
                    depth = 1;
                    id = 3068;
                    knowid = 6583;
                    name = "\U6709\U7406\U6570";
                    pid = 6582;
                    pidA = "0-2-13-6582";
                    qNum = 35487;
                    son = 1;
                    son1 =                     (
                                                {
                            depth = 2;
                            id = 3069;
                            knowid = 6584;
                            name = "\U6709\U7406\U6570\U7684\U51e0\U4e2a\U6982\U5ff5";
                            pid = 6583;
                            pidA = "0-2-13-6582-6583";
                            qNum = 14812;
                            son = 1;
                            son1 =                             (
                                                                {
                                    depth = 3;
                                    id = 3070;
                                    knowid = 6585;
                                    name = "\U6b63\U6570\U4e0e\U8d1f\U6570";
                                    pid = 6584;
                                    pidA = "0-2-13-6582-6583-6584";
                                    qNum = 2276;
                                    son = 0;
                                    url = "http://192.168.0.200//api.php/task1_2/taskadd1_2.html?id=6585&uid=12709";
                                },
.....................................................................
.....................................................................
复制代码

创建知识点实例KJPoint如下:

复制代码
//  KJPoint.h
//  Point
//  Created by mac on 16/6/21.
//  Copyright © 2016年 mac. All rights reserved.

#import <Foundation/Foundation.h>

@interface KJPoint : NSObject

@property (copy,nonatomic)NSString  *point_depth;  //知识点深度
@property (assign,nonatomic)BOOL    point_expand;  //知识点展开与否

@property (copy,nonatomic)NSString  *point_id;
@property (copy,nonatomic)NSString  *point_knowid;
@property (copy,nonatomic)NSString  *point_name;
@property (copy,nonatomic)NSString  *point_pid;
@property (copy,nonatomic)NSString  *point_pidA;
@property (copy,nonatomic)NSString  *point_qNum;
@property (copy,nonatomic)NSString  *point_url;
@property (copy,nonatomic)NSString  *point_son;  //作为判断是否有子节点的条件
@property (strong,nonatomic)NSArray *point_son1; //子知识点

-(instancetype)initWithPointDic:(NSDictionary *)pointDic;

@end



//  KJPoint.m
//  Point
//  Created by mac on 16/6/21.
//  Copyright © 2016年 mac. All rights reserved.

#import "KJPoint.h"

@implementation KJPoint

-(instancetype)initWithPointDic:(NSDictionary *)pointDic{
    self = [super init];
    if (self) {
        _point_id = [pointDic[@"id"] copy];
        _point_depth = [pointDic[@"depth"] copy];
        _point_knowid = [pointDic[@"knowid"] copy];
        _point_name = [pointDic[@"name"] copy];
        _point_pid = [pointDic[@"pid"] copy];
        _point_pidA = [pointDic[@"pidA"] copy];
        _point_url = [pointDic[@"url"] copy];
        _point_qNum = [pointDic[@"qNum"] copy];
        _point_son = [pointDic[@"son"] copy];
        _point_son1 = pointDic[@"son1"];
    }
    return self;
}

@end
复制代码

创建控制器实现效果,KJPointViewController类

复制代码
//  ViewController.h
//  Point
//  Created by mac on 16/6/21.
//  Copyright © 2016年 mac. All rights reserved.

#import <UIKit/UIKit.h>
@interface KJPonitViewController : UIViewController
@end


//  ViewController.m
//  Point
//
//  Created by mac on 16/6/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "KJPonitViewController.h"
#import "HttpTool/KJHttpTool.h"
#import "KJPoint.h"
#import "TreeTableView.h"
#import "Node.h"
@interface KJPonitViewController ()<TreeTableCellDelegate>
@property (strong,nonatomic)NSMutableArray *Points;
@property (strong,nonatomic)NSMutableArray *allPoints;
@end

@implementation KJPonitViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //获取知识点
    [self initPoints];
}

-(void)initPoints{
 
    //封装参数
    _Points = [NSMutableArray array];
    _allPoints = [NSMutableArray array];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"uid"] = @"12709";  //用户id
    params[@"grade"] = @"0";    //学段:0代表初中、1代表高中//发送请求
    [KJHttpTool getWithURL:Point_URL params:params success:^(id json) {
        NSLog(@"%@",json);
        
        [json[@"data"] enumerateObjectsUsingBlock:^(NSDictionary *pointDic, NSUInteger idx, BOOL * _Nonnull stop) {
           KJPoint *point = [[KJPoint alloc]initWithPointDic:pointDic];
           point.point_depth = @"0";
           point.point_expand = YES;
           [_Points addObject:point];
        }];
        [self recursiveAllPoints:_Points];
        
//        //打印输出
//        [_allPoints enumerateObjectsUsingBlock:^(KJPoint *point, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSLog(@"%@____%@___%d",point.point_name,point.point_depth,point.point_expand);
//        }];
        
        //创建Node节点
        NSMutableArray *nodes = [NSMutableArray array];
        [_allPoints enumerateObjectsUsingBlock:^(KJPoint *point, NSUInteger idx, BOOL * _Nonnull stop) {

//            NSLog(@"%@----%@----%@",point.point_name,point.point_knowid,point.point_pid);
            
            Node *node = [[Node alloc] initWithParentId:[point.point_pid integerValue] nodeId:[point.point_knowid integerValue] name:point.point_name depth:[point.point_depth integerValue] expand:point.point_expand];
            [nodes addObject:node];
        }];
        
        //TreeTableView
        TreeTableView *tableview = [[TreeTableView alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-20) withData:nodes];
        tableview.treeTableCellDelegate = self;
        [self.view addSubview:tableview];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

//递归所有的知识点
-(void)recursiveAllPoints:(NSMutableArray *)point_arrM{
    
    [point_arrM enumerateObjectsUsingBlock:^(KJPoint *point, NSUInteger idx, BOOL * _Nonnull stop) {
        [_allPoints addObject:point];
        
        if ([point.point_son isEqualToString:@"1"]) {
            NSMutableArray *sonPoints = [NSMutableArray array];
            [point.point_son1 enumerateObjectsUsingBlock:^(NSDictionary *pointDic, NSUInteger idx, BOOL * _Nonnull stop) {
                KJPoint *point = [[KJPoint alloc]initWithPointDic:pointDic];
                point.point_expand = NO;
                [sonPoints addObject:point];
            }];
            [self recursiveAllPoints:sonPoints];
        }
    }];
}

#pragma mark - TreeTableCellDelegate
-(void)cellClick:(Node *)node{
    NSLog(@"%@----%ld-----%ld------%ld",node.name,node.parentId,(long)node.nodeId,(long)node.depth);
}
@end
复制代码

说明:以后不论参数怎么变,不论是三级还是四级表格,都可以自动轻松实现

效果截图如下:

  

提示:自己先和后台商量好如何json数据格式如何返回,目的是方便自己整理

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5606697.html ,如需转载请自行联系原作者

相关文章
|
8天前
|
数据采集 前端开发 API
SurfGen爬虫:解析HTML与提取关键数据
SurfGen爬虫:解析HTML与提取关键数据
|
13天前
|
数据采集 监控 搜索推荐
深度解析淘宝商品详情API接口:解锁电商数据新维度,驱动业务增长
淘宝商品详情API接口,是淘宝开放平台为第三方开发者提供的一套用于获取淘宝、天猫等电商平台商品详细信息的应用程序接口。该接口涵盖了商品的基本信息(如标题、价格、图片)、属性参数、库存状况、销量评价、物流信息等,是电商企业实现商品管理、市场分析、营销策略制定等功能的得力助手。
|
23天前
|
搜索推荐 API 开发者
深度解析:利用商品详情 API 接口实现数据获取与应用
在电商蓬勃发展的今天,数据成为驱动业务增长的核心。商品详情API接口作为连接海量商品数据的桥梁,帮助运营者、商家和开发者获取精准的商品信息(如价格、描述、图片、评价等),优化策略、提升用户体验。通过理解API概念、工作原理及不同平台特点,掌握获取权限、构建请求、处理响应和错误的方法,可以将数据应用于商品展示、数据分析、竞品分析和个性化推荐等场景,助力电商创新与发展。未来,随着技术进步,API接口将与人工智能、大数据深度融合,带来更多变革。
62 3
|
27天前
|
存储 搜索推荐 大数据
数据大爆炸:解析大数据的起源及其对未来的启示
数据大爆炸:解析大数据的起源及其对未来的启示
92 15
数据大爆炸:解析大数据的起源及其对未来的启示
|
30天前
|
JSON 小程序 UED
微信小程序 app.json 配置文件解析与应用
本文介绍了微信小程序中 `app.json` 配置文件的详细
138 12
|
30天前
|
JSON 缓存 API
解析电商商品详情API接口系列,json数据示例参考
电商商品详情API接口是电商平台的重要组成部分,提供了商品的详细信息,支持用户进行商品浏览和购买决策。通过合理的API设计和优化,可以提升系统性能和用户体验。希望本文的解析和示例能够为开发者提供参考,帮助构建高效、可靠的电商系统。
39 12
|
1月前
|
存储 分布式计算 Hadoop
基于Java的Hadoop文件处理系统:高效分布式数据解析与存储
本文介绍了如何借鉴Hadoop的设计思想,使用Java实现其核心功能MapReduce,解决海量数据处理问题。通过类比图书馆管理系统,详细解释了Hadoop的两大组件:HDFS(分布式文件系统)和MapReduce(分布式计算模型)。具体实现了单词统计任务,并扩展支持CSV和JSON格式的数据解析。为了提升性能,引入了Combiner减少中间数据传输,以及自定义Partitioner解决数据倾斜问题。最后总结了Hadoop在大数据处理中的重要性,鼓励Java开发者学习Hadoop以拓展技术边界。
50 7
|
1月前
|
JSON 前端开发 搜索推荐
关于商品详情 API 接口 JSON 格式返回数据解析的示例
本文介绍商品详情API接口返回的JSON数据解析。最外层为`product`对象,包含商品基本信息(如id、name、price)、分类信息(category)、图片(images)、属性(attributes)、用户评价(reviews)、库存(stock)和卖家信息(seller)。每个字段详细描述了商品的不同方面,帮助开发者准确提取和展示数据。具体结构和字段含义需结合实际业务需求和API文档理解。
|
2月前
|
JSON JavaScript 前端开发
一次采集JSON解析错误的修复
两段采集来的JSON格式数据存在格式问题,直接使用PHP的`json_decode`会报错。解决思路包括:1) 手动格式化并逐行排查错误;2) 使用PHP-V8JS扩展在JavaScript环境中解析。具体方案一是通过正则表达式和字符串替换修复格式,方案二是利用V8Js引擎执行JS代码并返回JSON字符串,最终实现正确解析。 简介: 两段采集的JSON数据因掺杂JavaScript代码导致PHP解析失败。解决方案包括手动格式化修复和使用PHP-V8JS扩展在JavaScript环境中解析,确保JSON数据能被正确处理。
|
2月前
|
存储 安全 数据安全/隐私保护
深入解析iOS 14隐私保护功能:用户数据安全的新里程碑
随着数字时代的到来,个人隐私保护成为全球关注的焦点。苹果公司在最新的iOS 14系统中引入了一系列创新的隐私保护功能,旨在为用户提供更透明的数据使用信息和更强的控制权。本文将深入探讨iOS 14中的几项关键隐私功能,包括App跟踪透明性、简化的隐私设置以及增强的系统安全性,分析它们如何共同作用以提升用户的隐私保护水平。
191 3

热门文章

最新文章

  • 1
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 2
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 3
    DeepSeek技术报告解析:为什么DeepSeek-R1 可以用低成本训练出高效的模型
  • 4
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 5
    DeepSeek模型的突破:性能超越R1满血版的关键技术解析
  • 6
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 7
    深度解析淘宝商品详情API接口:解锁电商数据新维度,驱动业务增长
  • 8
    阿里云服务器经济型e实例解析:性能、稳定性与兼顾成本
  • 9
    小红书笔记详情 API 接口:获取、应用与收益全解析
  • 10
    阿里云CDN怎么收费?看这一篇就够了,CDN不同计费模式收费价格全解析
  • 推荐镜像

    更多