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
//
// SelectYetTableViewCell.m
// redstar
//
// Created by admin on 15/12/21.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "SelectYetTableViewCell.h"
#import "TreeNodeModel.h"
@implementation SelectYetTableViewCell
- (void)setSelectTreeArray:(NSMutableArray *)selectTreeArray
{
for (UIButton *button in self.contentView.subviews) {
[button removeFromSuperview];
}
_selectTreeArray = selectTreeArray;
for (int i = 0; i < selectTreeArray.count; i++) {
TreeNodeModel *model = selectTreeArray[i];
UIButton *button = [[UIButton alloc] init];
button.tag = 6666687 + i;
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:model.name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:14.0];
button.backgroundColor = kSelectStoreColor;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 8);
[self.contentView addSubview:button];
CGFloat x,y;
CGFloat marginX = 5;
CGFloat marginY = 5;
CGFloat w = (kScreenWidth - 80) / 2 - marginX;
CGFloat h = 25;
NSInteger row;
if ((i + 1) % 2 == 0) {
x = -20;
row = (i - 1) / 2;
y = 10 + (i / 2) * (h + marginY);
NSLayoutConstraint *imageViewRight = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:x];
[self.contentView addConstraint:imageViewRight];
} else {
x = 60;
row = i / 2;
y = 10 + (i / 2) * (h + marginY);
NSLayoutConstraint *imageViewLeft = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:x];
[self.contentView addConstraint:imageViewLeft];
}
NSLayoutConstraint *imageViewTop = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:y];
[self.contentView addConstraint:imageViewTop];
NSLayoutConstraint *imageViewHeight = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:h];
[self.contentView addConstraint:imageViewHeight];
NSLayoutConstraint *imageViewWidth = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:w];
[self.contentView addConstraint:imageViewWidth];
if (i == (_selectTreeArray.count - 1)) {
NSLayoutConstraint *imageViewBootom = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5];
[self.contentView addConstraint:imageViewBootom];
}
// 删除按钮
UIButton *deleteBtn = [[UIButton alloc] init];
deleteBtn.translatesAutoresizingMaskIntoConstraints = NO;
[deleteBtn setImage:[UIImage imageNamed:@"delete_box"] forState:UIControlStateNormal];
[deleteBtn addTarget:self action:@selector(deleteBox:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:deleteBtn];
NSLayoutConstraint *deleteTop = [NSLayoutConstraint constraintWithItem:deleteBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeTop multiplier:1.0 constant:3];
[button addConstraint:deleteTop];
NSLayoutConstraint *deleteHeight = [NSLayoutConstraint constraintWithItem:deleteBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:7.5];
[button addConstraint:deleteHeight];
NSLayoutConstraint *deleteWidth = [NSLayoutConstraint constraintWithItem:deleteBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:7.5];
[button addConstraint:deleteWidth];
NSLayoutConstraint *deleteRight = [NSLayoutConstraint constraintWithItem:deleteBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeRight multiplier:1.0 constant:-3];
[button addConstraint:deleteRight];
}
}
- (void)deleteBox:(UIButton *)sender
{
if (_delegate && [_delegate respondsToSelector:@selector(removeCurrentWithButton:)]) {
[_delegate removeCurrentWithButton:sender];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end