forked from BijeshNair/NIDropDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNIDropDown.m
118 lines (96 loc) · 3.69 KB
/
NIDropDown.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
//
// NIDropDown.m
// NIDropDown
//
// Created by Bijesh N on 12/28/12.
// Copyright (c) 2012 Nitor Infotech. All rights reserved.
//
#import "NIDropDown.h"
#import "QuartzCore/QuartzCore.h"
@interface NIDropDown ()
@property(nonatomic, strong) UITableView *table;
@property(nonatomic, strong) UIButton *btnSender;
@property(nonatomic, retain) NSArray *list;
@end
@implementation NIDropDown
@synthesize table;
@synthesize btnSender;
@synthesize list;
@synthesize delegate;
- (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr {
btnSender = b;
self = [super init];
if (self) {
// Initialization code
CGRect btn = b.frame;
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
self.list = [NSArray arrayWithArray:arr];
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 8;
self.layer.shadowOffset = CGSizeMake(-5, 5);
self.layer.shadowRadius = 5;
self.layer.shadowOpacity = 0.5;
table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
table.delegate = self;
table.dataSource = self;
table.layer.cornerRadius = 5;
table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
table.separatorColor = [UIColor grayColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height);
table.frame = CGRectMake(0, 0, btn.size.width, *height);
[UIView commitAnimations];
[b.superview addSubview:self];
[self addSubview:table];
}
return self;
}
-(void)hideDropDown:(UIButton *)b {
CGRect btn = b.frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
table.frame = CGRectMake(0, 0, btn.size.width, 0);
[UIView commitAnimations];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
cell.textLabel.text =[list objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor whiteColor];
UIView * v = [[UIView alloc] init];
v.backgroundColor = [UIColor grayColor];
cell.selectedBackgroundView = v;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self hideDropDown:btnSender];
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
[btnSender setTitle:c.textLabel.text forState:UIControlStateNormal];
[self myDelegate];
}
- (void) myDelegate {
[self.delegate niDropDownDelegateMethod:self];
}
-(void)dealloc {
[super dealloc];
[table release];
[self release];
}
@end