本文实例为大家分享了简单封装的iOS下拉选择菜单代码,供大家参考,具体内容如下
?
|
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
|
//
// OrderListDownMenu.h
#import <UIKit/UIKit.h>
@protocol OrderListDownMenuDelegate <NSObject>
- (void)OrderListDownMenu:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
@end
typedef void(^Dismiss)(void);
@interface OrderListDownMenu : UIView<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) id<OrderListDownMenuDelegate> delegate;
@property (nonatomic, strong) NSArray *arrData;
@property (nonatomic, strong) NSArray *arrImgName;
@property (nonatomic, copy) Dismiss dismiss;
- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight;
- (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion;
@end
|
?
|
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
|
#import "OrderListDownMenu.h"
#define TopToView 63.0f
#define rightToView kScreenWidth - 15.0f
#define LeftToView kScreenWidth - 145.0 - 10.0f
#define CellLineEdgeInsets UIEdgeInsetsMake(0, -80, 0, 0)
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface OrderListDownMenu()
@property (nonatomic, assign) CGPoint origin;
@property (nonatomic, assign) CGFloat rowHeight;
@end
@implementation OrderListDownMenu
- (instancetype)initWithDataArr:(NSArray *)dataArr origin:(CGPoint)origin width:(CGFloat)width rowHeight:(CGFloat)rowHeight {
self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
if (self) {
if (rowHeight <= 0) {
rowHeight = 50;
}
// 设置背景颜色
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
self.origin = origin;
self.rowHeight = rowHeight;
self.arrData = [dataArr copy];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + LeftToView, origin.y + TopToView, width, rowHeight * dataArr.count) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self addSubview:_tableView];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.layer.cornerRadius = 2;
_tableView.bounces = NO;
_tableView.layer.cornerRadius = 8;
_tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];
_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:CellLineEdgeInsets];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:CellLineEdgeInsets];
}
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrData.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return self.rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.textColor = THEME_COLOR_GRAY_1;
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.text = self.arrData[indexPath.row];
if (self.arrImgName.count > indexPath.row) {
cell.imageView.image = [UIImage imageNamed:self.arrImgName[indexPath.row]];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
}
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 49, _tableView.frame.size.width, 0.5);
label.backgroundColor = THEME_SEPARATOR_COLOR;
[cell.contentView addSubview:label];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([self.delegate respondsToSelector:@selector(OrderListDownMenu:didSelectRowAtIndexPath:)]){
[self.delegate OrderListDownMenu:tableView didSelectRowAtIndexPath:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self dismissWithCompletion:nil];
}
- (void)dismissWithCompletion:(void (^)(OrderListDownMenu *object))completion {
__weak __typeof(self) weakSelf = self;
[UIView animateWithDuration:0.2 animations:^{
weakSelf.alpha = 0;
weakSelf.tableView.frame = CGRectMake(weakSelf.origin.x + LeftToView + 145, weakSelf.origin.y + TopToView, 0, 0);
} completion:^(BOOL finished) {
[weakSelf removeFromSuperview];
if (completion) {
completion(weakSelf);
}
if (weakSelf.dismiss) {
weakSelf.dismiss();
}
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if (![touch.view isEqual:self.tableView]) {
[self dismissWithCompletion:nil];
}
}
- (void)drawRect:(CGRect)rect {
//[colors[serie] setFill];
//拿到当前视图准备好的画板
CGContextRef context = UIGraphicsGetCurrentContext();
//利用path进行绘制三角形
CGContextBeginPath(context);//标记
CGContextMoveToPoint(context,
rightToView - 13, 53);//设置起点
CGContextAddLineToPoint(context,
rightToView - 21, TopToView);
CGContextAddLineToPoint(context,
rightToView - 4, TopToView);
CGContextClosePath(context);//路径结束标志,不写默认封闭
[self.tableView.backgroundColor setFill]; //设置填充色
[self.tableView.backgroundColor setStroke]; //设置边框颜色
CGContextDrawPath(context,
kCGPathFillStroke);//绘制路径path
}
@end
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 35
-
2025-05-25 41
-
2025-05-27 15
-
2025-05-25 33
-
2025-06-04 21
热门评论

