1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
56
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
99
100
101
102
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// TreeNodeCell.m
// redstar
//
// Created by admin on 15/12/18.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "TreeNodeCell.h"
#import "CheckBoxButton.h"
@interface TreeNodeCell ()
@end
@implementation TreeNodeCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#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)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self configSelf];
}
return self;
}
-(void)configSelf {
}
#pragma mark - 属性
-(void)setNodeData:(TreeNodeModel *)nodeData {
_nodeData = nodeData;
[self configFoldButton];
[self configTitleButton];
}
#pragma mark - 折叠按钮
- (void)configFoldButton {
if (self.foldButton == nil) {
self.foldButton = [UIButton buttonWithType:UIButtonTypeCustom];
_foldButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.foldButton addTarget:self action:@selector(actionFoldButton:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.foldButton];
NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_foldButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[self.contentView addConstraint:tableTop];
NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_foldButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
[self.contentView addConstraint:tableLeft];
NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_foldButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
[self.contentView addConstraint:tableRight];
NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_foldButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self.contentView addConstraint:tableBottom];
NSLayoutConstraint *tableHeight = [NSLayoutConstraint constraintWithItem:_foldButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];
[self.contentView addConstraint:tableHeight];
}
self.foldButton.selected = self.nodeData.expand;
}
-(void)actionFoldButton:(UIButton *)sender {
if ([self.nodeDelegate respondsToSelector:@selector(shouldClickFoldButtonAtNode:)]) {
if ([self.nodeDelegate shouldClickFoldButtonAtNode:self] == NO) {
return;
}
}
sender.selected = ! sender.selected;
self.nodeData.expand = sender.selected;
if ([self.nodeDelegate respondsToSelector:@selector(didClickFoldButtonAtNode:)]) {
[self.nodeDelegate didClickFoldButtonAtNode:self];
}
}
#pragma mark - 标题按钮
-(void)configTitleButton {
[self.titleButton removeFromSuperview];
self.titleButton = [[CheckBoxButton alloc] init];
_titleButton.translatesAutoresizingMaskIntoConstraints = NO;
self.titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self.titleButton addTarget:self action:@selector(actionTitleButton:) forControlEvents:UIControlEventTouchUpInside];
[_titleButton setImage:[UIImage imageNamed:@"uncheck_box"] forState:UIControlStateNormal];
[_titleButton setImage:[UIImage imageNamed:@"check_box"] forState:UIControlStateSelected];
[self.titleButton setTitle:self.nodeData.name forState:UIControlStateNormal];
[self.titleButton setTitleColor:kLightBlack forState:UIControlStateNormal];
[self.titleButton setTitleColor:kSelectStoreColor forState:UIControlStateSelected];
self.titleButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
[self addSubview:self.titleButton];
//CGFloat x = 20 + self.nodeData.levelDeep * 22;
CGFloat x = self.nodeData.levelDeep * 22;
NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[self addConstraint:tableTop];
NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:x];
[self addConstraint:tableLeft];
NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
[self addConstraint:tableBottom];
if (_arrowImageView) {
[_arrowImageView removeFromSuperview];
_arrowImageView = nil;
}
self.arrowImageView = [[UIImageView alloc] init];
_arrowImageView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_arrowImageView];
if (self.nodeData.levelDeep == 1) {
_arrowImageView.image = [UIImage imageNamed:@"arrow_down"];
NSLayoutConstraint *arrowWidth = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:12.5];
[self addConstraint:arrowWidth];
} else if (self.nodeData.levelDeep == 2) {
_arrowImageView.image = [UIImage imageNamed:@"grey-trilateral_down"];
NSLayoutConstraint *arrowWidth = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:11];
[self addConstraint:arrowWidth];
} else if (self.nodeData.levelDeep == 3){
//_arrowImageView.image = [UIImage imageNamed:@"white_background"];
_arrowImageView.hidden = YES;
NSLayoutConstraint *arrowWidth = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:15];
[self addConstraint:arrowWidth];
}
NSLayoutConstraint *arrowTop = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:21.5];
[self addConstraint:arrowTop];
NSLayoutConstraint *arrowRight = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
[self addConstraint:arrowRight];
NSLayoutConstraint *arrowHeight = [NSLayoutConstraint constraintWithItem:_arrowImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:7.5];
[self addConstraint:arrowHeight];
}
-(void)actionTitleButton:(UIButton *)sender {
if ([self.nodeDelegate respondsToSelector:@selector(didClickTitleAtNode:)]) {
[self.nodeDelegate didClickTitleAtNode:self];
}
}
@end