-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUITableView+Common.m
executable file
·259 lines (190 loc) · 10.5 KB
/
UITableView+Common.m
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
//
// UITableView+Common.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-5.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "UITableView+Common.h"
@implementation UITableView (Common)
- (void)addRadiusforCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(tintColor)]) {
CGFloat cornerRadius = 5.f;
cell.backgroundColor = UIColor.clearColor;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [self numberOfRowsInSection:indexPath.section]-1) {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (indexPath.row == 0) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
} else if (indexPath.row == [self numberOfRowsInSection:indexPath.section]-1) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
}
layer.path = pathRef;
CFRelease(pathRef);
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
if (cell.backgroundColor) {
layer.fillColor = cell.backgroundColor.CGColor;//layer的填充色用cell原本的颜色
}else if (cell.backgroundView && cell.backgroundView.backgroundColor){
layer.fillColor = cell.backgroundView.backgroundColor.CGColor;
}else{
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
}
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+2, bounds.size.height-lineHeight, bounds.size.width-2, lineHeight);
lineLayer.backgroundColor = self.separatorColor.CGColor;
[layer addSublayer:lineLayer];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;
}
}
- (void)addLineforPlainCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath withLeftSpaceAndSectionLine:(CGFloat)leftSpace{
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
CGPathAddRect(pathRef, nil, bounds);
layer.path = pathRef;
CFRelease(pathRef);
if (cell.backgroundColor) {
layer.fillColor = cell.backgroundColor.CGColor;//layer的填充色用cell原本的颜色
}else if (cell.backgroundView && cell.backgroundView.backgroundColor){
layer.fillColor = cell.backgroundView.backgroundColor.CGColor;
}else{
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
}
CGColorRef lineColor = kColorDDD.CGColor;
//判断整个tableview 最后的元素
if ((self.numberOfSections==(indexPath.section+1))&&indexPath.row == [self numberOfRowsInSection:indexPath.section]-1) {
//上短,下长
// [self layer:layer addLineUp:TRUE andLong:YES andColor:lineColor andBounds:bounds withLeftSpace:leftSpace];
[self layer:layer addLineUp:NO andLong:YES andColor:lineColor andBounds:bounds withLeftSpace:0];
}else
{
[self layer:layer addLineUp:NO andLong:NO andColor:lineColor andBounds:bounds withLeftSpace:leftSpace];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
cell.backgroundView = testView;
}
- (void)addLineforPlainCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath withLeftSpace:(CGFloat)leftSpace hasSectionLine:(BOOL)hasSectionLine{
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 0, 0);
CGPathAddRect(pathRef, nil, bounds);
layer.path = pathRef;
CFRelease(pathRef);
if (cell.backgroundColor) {
layer.fillColor = cell.backgroundColor.CGColor;//layer的填充色用cell原本的颜色
}else if (cell.backgroundView && cell.backgroundView.backgroundColor){
layer.fillColor = cell.backgroundView.backgroundColor.CGColor;
}else{
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
}
CGColorRef lineColor = kColorD8DDE4.CGColor;
CGColorRef sectionLineColor = lineColor;
if (indexPath.row == 0 && indexPath.row == [self numberOfRowsInSection:indexPath.section]-1) {
//只有一个cell。加上长线&下长线
if (hasSectionLine) {
[self layer:layer addLineUp:YES andLong:YES andColor:sectionLineColor andBounds:bounds withLeftSpace:0];
[self layer:layer addLineUp:NO andLong:YES andColor:sectionLineColor andBounds:bounds withLeftSpace:0];
}
} else if (indexPath.row == 0) {
//第一个cell。加上长线&下短线
if (hasSectionLine) {
[self layer:layer addLineUp:YES andLong:YES andColor:sectionLineColor andBounds:bounds withLeftSpace:0];
}
[self layer:layer addLineUp:NO andLong:NO andColor:lineColor andBounds:bounds withLeftSpace:leftSpace];
} else if (indexPath.row == [self numberOfRowsInSection:indexPath.section]-1) {
//最后一个cell。加下长线
if (hasSectionLine) {
[self layer:layer addLineUp:NO andLong:YES andColor:sectionLineColor andBounds:bounds withLeftSpace:0];
}
} else {
//中间的cell。只加下短线
[self layer:layer addLineUp:NO andLong:NO andColor:lineColor andBounds:bounds withLeftSpace:leftSpace];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
cell.backgroundView = testView;
}
- (void)addLineforPlainCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath withLeftSpace:(CGFloat)leftSpace{
[self addLineforPlainCell:cell forRowAtIndexPath:indexPath withLeftSpace:leftSpace hasSectionLine:YES];
}
- (void)layer:(CALayer *)layer addLineUp:(BOOL)isUp andLong:(BOOL)isLong andColor:(CGColorRef)color andBounds:(CGRect)bounds withLeftSpace:(CGFloat)leftSpace{
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.0f / [UIScreen mainScreen].scale);
CGFloat left, top;
if (isUp) {
top = 0;
}else{
top = bounds.size.height-lineHeight;
}
if (isLong) {
left = 0;
}else{
left = leftSpace;
}
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+left, top, bounds.size.width-left, lineHeight);
lineLayer.backgroundColor = color;
[layer addSublayer:lineLayer];
}
- (UITapImageView *)getHeaderViewWithStr:(NSString *)headerStr andBlock:(void(^)(id obj))tapAction{
return [self getHeaderViewWithStr:headerStr color:kColorTableSectionBg andBlock:tapAction];
}
- (UITapImageView *)getHeaderViewWithStr:(NSString *)headerStr color:(UIColor *)color andBlock:(void(^)(id obj))tapAction{
UITapImageView *headerView = [[UITapImageView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width,30)];
[headerView setImage:[UIImage imageWithColor:color]];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, kScreen_Width-20, CGRectGetHeight(headerView.frame))];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = kColor999;
if (kDevice_Is_iPhone6Plus) {
headerLabel.font = [UIFont systemFontOfSize:14];
}else{
headerLabel.font = [UIFont systemFontOfSize:kScaleFrom_iPhone5_Desgin(12)];
}
headerLabel.text = headerStr;
[headerView addSubview:headerLabel];
[headerView addTapBlock:tapAction];
return headerView;
}
- (UITapImageView *)getHeaderViewWithStr:(NSString *)headerStr color:(UIColor *)color leftNoticeColor:(UIColor*)noticeColor andBlock:(void(^)(id obj))tapAction{
UITapImageView *headerView = [[UITapImageView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width,44)];
[headerView setImage:[UIImage imageWithColor:color]];
UIView* noticeView=[[UIView alloc] initWithFrame:CGRectMake(12, 14, 3, 16)];
noticeView.backgroundColor=noticeColor;
[headerView addSubview:noticeView];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(12+3+10, 7, kScreen_Width-20, 30)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = kColor999;
if (kDevice_Is_iPhone6Plus) {
headerLabel.font = [UIFont systemFontOfSize:14];
}else{
headerLabel.font = [UIFont systemFontOfSize:kScaleFrom_iPhone5_Desgin(12)];
}
CGFloat lineHeight = (1.0f / [UIScreen mainScreen].scale);
UIView *seperatorline = [[UIView alloc] initWithFrame:CGRectMake(0, 44-lineHeight,kScreen_Width , lineHeight)];
seperatorline.backgroundColor = kColorDDD;
[headerView addSubview:seperatorline];
headerLabel.text = headerStr;
[headerView addSubview:headerLabel];
[headerView addTapBlock:tapAction];
return headerView;
}
@end