TreeView.m 5.01 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12
//
//  TreeView.m
//  redstar
//
//  Created by admin on 15/12/18.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "TreeView.h"
#import "TreeNodeCell.h"
#import "SelectYetTableViewCell.h"

admin's avatar
admin committed
13 14
#define kTreeNodeCell @"nodelCell"

admin's avatar
admin committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
@interface TreeView () <SelectYetDelegate>
@property (nonatomic) NSArray *nodeArray;
@end

@implementation TreeView
#pragma mark - 初始化函数
-(instancetype)init {
    self = [super init];
    if (self) {
        [self configSelf];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self configSelf];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSelf];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self configSelf];
    }
    return self;
}

-(void)configSelf {
    self.dataSource = self;
    self.delegate = self;
admin's avatar
admin committed
56
    [self registerClass:[TreeNodeCell class] forCellReuseIdentifier:kTreeNodeCell];
admin's avatar
admin committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    self.rowHeight = UITableViewAutomaticDimension;
    self.estimatedRowHeight = 50.0;
}
#pragma mark - SelectYetDelegate
- (void)removeCurrentWithButton:(UIButton *)sender
{
    UIButton *button = (UIButton *)sender.superview;
    NSInteger index = button.tag - 6666687;
    TreeNodeModel *treeM = _selectTreeArray[index];
    treeM.isChoose = NO;
    [button removeFromSuperview];
    button = nil;
    [self.selectTreeArray removeObject:treeM];
    
    [self reloadData];
}

#pragma mark - 表格代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.nodeArray = [self.nodeData nodeArray:NO];
    return self.nodeArray.count + 1;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        SelectYetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"11nodeCell"];
        if (cell == nil) {
            cell = [[SelectYetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"11nodeCell"];
        }
        cell.delegate = self;
        cell.selectTreeArray = self.selectTreeArray;
        cell.textLabel.text = @"已选:";
        cell.textLabel.textColor = kLightGray;
        cell.textLabel.font = [UIFont systemFontOfSize:15.0];
        return cell;
    } else {
admin's avatar
admin committed
99 100
        TreeNodeCell *cell = [tableView dequeueReusableCellWithIdentifier:kTreeNodeCell];
        
admin's avatar
admin committed
101
        if (cell == nil) {
admin's avatar
admin committed
102
            cell = [[TreeNodeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTreeNodeCell];
admin's avatar
admin committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }
        
        cell.nodeDelegate = self;
        cell.nodeData = self.nodeArray[indexPath.row - 1];
        // 强制刷新,主要是为了刷新那些自画线条
        
        cell.titleButton.selected = cell.nodeData.isChoose;
        [cell setNeedsDisplay];
        return cell;
    }
}

#pragma mark - node代理
-(BOOL)shouldClickFoldButtonAtNode:(TreeNodeCell *)node {
    if ([self.treeDelegate respondsToSelector:@selector(tree:shouldClickFoldButtonAtNode:)]) {
        return [self.treeDelegate tree:self shouldClickFoldButtonAtNode:node];
    }
    else {
        return YES;
    }
}

-(void)didClickFoldButtonAtNode:(TreeNodeCell *)node {
    // 获取变动的行索引
    NSUInteger beginIndex = [self.nodeArray indexOfObject:node.nodeData] + 2;
    NSUInteger indexCount = [[node.nodeData nodeArray:NO] count];
    NSMutableArray *indexArray = [NSMutableArray array];
    for (int i = 0; i < indexCount; i++) {
        [indexArray addObject:[NSIndexPath indexPathForRow:beginIndex + i inSection:0]];
    }
    // 用插入和删除函数就会产生动画
    if (node.nodeData.isExpand == NO) {
admin's avatar
admin committed
135 136 137 138 139 140
        
        [UIView animateWithDuration:0.2 animations:^{
            node.arrowImageView.transform = CGAffineTransformIdentity;
            node.arrowImageView.transform = CGAffineTransformMakeRotation(0);
        }];
        
admin's avatar
admin committed
141 142 143
        [self deleteRowsAtIndexPaths:indexArray withRowAnimation:(UITableViewRowAnimationTop)];
    }
    else {
admin's avatar
admin committed
144 145 146 147 148 149
        
        [UIView animateWithDuration:0.2 animations:^{
            node.arrowImageView.transform = CGAffineTransformIdentity;
            node.arrowImageView.transform = CGAffineTransformMakeRotation(-M_PI);
        }];
        
admin's avatar
admin committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        [self insertRowsAtIndexPaths:indexArray withRowAnimation:(UITableViewRowAnimationBottom)];
    }
    
    if (beginIndex == 1) {
        [node setNeedsDisplay];
    }
    
    
    if ([self.treeDelegate respondsToSelector:@selector(tree:didClickFoldButtonAtNode:)]) {
        [self.treeDelegate tree:self didClickFoldButtonAtNode:node];
    }
}

- (void)didClickTitleAtNode:(TreeNodeCell *)node {
    if ([self.treeDelegate respondsToSelector:@selector(tree:didClickTitleAtNode:)]) {
        [self.treeDelegate tree:self didClickTitleAtNode:node];
    }
}


@end