iOS:二叉樹多級表格的使用,使用三方庫TreeTableView-master實現對json解析數據的遞歸遍歷整理成樹狀結構


在項目中,我們有時需要使用二叉樹來實現多級表格的遞歸遍歷查詢,如果對二叉樹比較懂,那么寫起來其實也不費事,為了節省開發時間,下面介紹一下第三方庫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數據格式如何返回,目的是方便自己整理


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM