CNLiveMineCell.m
12.7 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//
// CNLiveMineCell.m
// CNLiveMineModule
//
// Created by 153993236@qq.com on 11/12/2019.
// Copyright © 2019 153993236@qq.com. All rights reserved.
//
#import "CNLiveMineCell.h"
#import <Masonry/Masonry.h>
#import "QMUIKit.h"
#define HexColor(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface CNLiveMineCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *titleButton;
@property (nonatomic, strong) UIView *line;
@end
@implementation CNLiveMineCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.backgroundColor = HexColor(0xf4f4f4);
[self xw_layoutSubviews];
}
return self;
}
- (void)xw_layoutSubviews{
self.bgView = [[UIView alloc] init];
_bgView.backgroundColor = [UIColor whiteColor];
_bgView.layer.masksToBounds = YES;
_bgView.layer.cornerRadius = 5;
[self.contentView addSubview:self.bgView];
self.titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
_titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[_titleButton setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateNormal];
_titleButton.adjustsImageWhenHighlighted = NO;
// [_titleButton addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.titleButton];
self.line = [[UIView alloc] init];
_line.backgroundColor = HexColor(0xebebeb);
[self.contentView addSubview:self.line];
}
- (void)setModel:(CNLiveMineModel *)model{
_model = model;
[_titleButton setTitle:model.name forState:UIControlStateNormal];
UIImage *xw_image = [self xw_getImageName:model.pic bundle:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
[_titleButton setImage:xw_image forState:UIControlStateNormal];
switch (model.type) {
case CNLiveMineCellTypeOrder:
{
[self order_layoutSubviews];
}
break;
case CNLiveMineCellTypeTool:
{
[self tool_layoutSubviews];
}
break;
case CNLiveMineCellTypeGoods:
{
[self goods_layoutSubviews];
}
break;
case CNLiveMineCellTypeSports:
{
[self sports_layoutSubviews];
}
break;
}
}
- (void)order_layoutSubviews{
[_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
make.top.equalTo(self.contentView.mas_top).with.offset(0);
make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
}];
[_titleButton setImage:nil forState:UIControlStateNormal];
_titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
_titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//使图片和文字水平居中显示
[_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bgView.mas_top).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(50);
}];
[_line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(0.5);
}];
CGFloat width = SCREEN_WIDTH-2*self.model.margin;
CGFloat btnWidth = width/self.model.titles.count*1.0;
CGFloat btnHeight = 60;
for(int i = 0; i < self.model.titles.count; i++){
UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*i, 50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
[self addSubview:btn];
}
}
- (void)tool_layoutSubviews{
[_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
make.top.equalTo(self.contentView.mas_top).with.offset(0);
make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
}];
[_titleButton setImage:nil forState:UIControlStateNormal];
_titleButton.titleLabel.textAlignment = NSTextAlignmentLeft;
_titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//使图片和文字水平居中显示
[_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bgView.mas_top).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(50);
}];
[_line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(0.5);
}];
CGFloat width = SCREEN_WIDTH-2*self.model.margin;
CGFloat btnWidth = width/4.0;
CGFloat btnHeight = 60;
for(int i = 0; i < self.model.titles.count; i++){
NSInteger row = i % 4;
NSInteger col = floorf(i / 4.0);
UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*row, (col*(btnHeight+self.model.margin))+50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
[self addSubview:btn];
}
}
- (void)goods_layoutSubviews{
//图片相对于button的偏移量
[_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin/2.0, 0, 0)];
//文字相对于图片的偏移量
[_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin/2.0, 0, 0)];
[_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
make.top.equalTo(self.contentView.mas_top).with.offset(0);
make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
}];
_titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
_titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
[_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bgView.mas_top).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(50);
}];
[_line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(0.5);
}];
CGFloat width = SCREEN_WIDTH-2*self.model.margin;
CGFloat btnWidth = width/4.0;
CGFloat btnHeight = 80;
// 电子票
UIButton *btn = [self createButtonWithFrame:CGRectMake((width-btnWidth)/2.0, self.model.margin+50, btnWidth, btnHeight) title:self.model.titles[0] image:self.model.images[0] tag:10000];
[self addSubview:btn];
}
- (void)sports_layoutSubviews{
//图片相对于button的偏移量
[_titleButton setImageEdgeInsets:UIEdgeInsetsMake(0, -self.model.margin/2.0, 0, 0)];
//文字相对于图片的偏移量
[_titleButton setTitleEdgeInsets:UIEdgeInsetsMake(0, self.model.margin/2.0, 0, 0)];
[_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.contentView.mas_right).with.offset(-self.model.margin);
make.top.equalTo(self.contentView.mas_top).with.offset(0);
make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-self.model.margin);
}];
_titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
_titleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
[_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bgView.mas_top).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(50);
}];
[_line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleButton.mas_bottom).with.offset(0);
make.left.equalTo(self.bgView.mas_left).with.offset(self.model.margin);
make.right.equalTo(self.bgView.mas_right).with.offset(-self.model.margin);
make.height.with.offset(0.5);
}];
CGFloat width = SCREEN_WIDTH-2*self.model.margin;
CGFloat btnWidth = width/4.0;
CGFloat btnHeight = 60;
for(int i = 0; i < self.model.titles.count; i++){
NSInteger row = i % 4;
NSInteger col = floorf(i / 4.0);
UIButton *btn = [self createButtonWithFrame:CGRectMake(self.model.margin+btnWidth*row, (col*(btnHeight+self.model.margin))+50+self.model.margin, btnWidth, btnHeight) title:self.model.titles[i] image:self.model.images[i] tag:10000+i];
[self addSubview:btn];
}
}
- (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title image:(NSString *)image tag:(NSInteger)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = tag;
btn.frame = frame;
btn.titleLabel.font = [UIFont systemFontOfSize:14];
btn.titleLabel.textAlignment = NSTextAlignmentCenter;
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIImage *xw_image = [self xw_getImageName:image bundle:@"CNLiveMineModule" targetClass:[CNLiveMineCell class]];
[btn setImage:xw_image forState:UIControlStateNormal];
btn.adjustsImageWhenHighlighted = NO;
[btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
btn.titleEdgeInsets = UIEdgeInsetsMake(btn.imageView.frame.size.height+self.model.margin, -btn.imageView.frame.size.width, 0, 0);
btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.bounds.size.height, 0, self.model.margin, -btn.titleLabel.bounds.size.width);
return btn;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)btnDidClicked:(UIButton *)btn{
if (self.delegate && [self.delegate respondsToSelector:@selector(cell:clickedButtonWithType:tag:)]) {
[self.delegate cell:self clickedButtonWithType:self.model.type tag:btn.tag];
}
}
/**
* 创建图片
*
* @param imageName 图片名字
* @param bundleName 图片所在的bundle名字
* @param targetClass 类和bundle的同级目录
* @return 返回UIImage
*/
- (UIImage *)xw_getImageName:(NSString *)imageName bundle:(NSString *)bundleName targetClass:(Class)targetClass{
NSBundle *bundle = [NSBundle bundleForClass:targetClass];
NSURL *url = [bundle URLForResource:bundleName withExtension:@"bundle"];
NSBundle *targetBundle = [NSBundle bundleWithURL:url];
UIImage *image = [UIImage imageNamed:imageName inBundle:targetBundle compatibleWithTraitCollection:nil];
return image?image:[UIImage imageNamed:imageName inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
}
@end