NIDropDown.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // NIDropDown.m
  3. // NIDropDown
  4. //
  5. // Created by Bijesh N on 12/28/12.
  6. // Copyright (c) 2012 Nitor Infotech. All rights reserved.
  7. //
  8. #import "NIDropDown.h"
  9. #import "QuartzCore/QuartzCore.h"
  10. #import "iSalesDB.h"
  11. @interface NIDropDown ()
  12. @property(nonatomic, strong) UITableView *table;
  13. @property(nonatomic, strong) UIButton *btnSender;
  14. //@property(nonatomic, retain) NSArray *list;
  15. //@property(nonatomic, retain) NSArray *imageList;
  16. @end
  17. @implementation NIDropDown
  18. @synthesize table;
  19. @synthesize btnSender;
  20. //@synthesize list;
  21. //@synthesize imageList;
  22. @synthesize delegate;
  23. @synthesize animationDirection;
  24. //
  25. //- (id)showDropDown:(UIButton *)b:(CGFloat *)height:(NSArray *)arr:(NSArray *)imgArr:(NSString *)direction {
  26. // btnSender = b;
  27. // animationDirection = direction;
  28. // self.table = (UITableView *)[super init];
  29. // if (self) {
  30. // // Initialization code
  31. // CGRect btn = b.frame;
  32. // self.list = [NSArray arrayWithArray:arr];
  33. // self.imageList = [NSArray arrayWithArray:imgArr];
  34. // if ([direction isEqualToString:@"up"]) {
  35. // self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
  36. // self.layer.shadowOffset = CGSizeMake(-5, -5);
  37. // }else if ([direction isEqualToString:@"down"]) {
  38. // self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
  39. // self.layer.shadowOffset = CGSizeMake(-5, 5);
  40. // }
  41. //
  42. // self.layer.masksToBounds = NO;
  43. // self.layer.cornerRadius = 8;
  44. // self.layer.shadowRadius = 5;
  45. // self.layer.shadowOpacity = 0.5;
  46. //
  47. // table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
  48. // table.delegate = self;
  49. // table.dataSource = self;
  50. // table.layer.cornerRadius = 5;
  51. // table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
  52. // table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  53. // table.separatorColor = [UIColor grayColor];
  54. //
  55. // [UIView beginAnimations:nil context:nil];
  56. // [UIView setAnimationDuration:0.5];
  57. // if ([direction isEqualToString:@"up"]) {
  58. // self.frame = CGRectMake(btn.origin.x, btn.origin.y-*height, btn.size.width, *height);
  59. // } else if([direction isEqualToString:@"down"]) {
  60. // self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, *height);
  61. // }
  62. // table.frame = CGRectMake(0, 0, btn.size.width, *height);
  63. // [UIView commitAnimations];
  64. // [b.superview addSubview:self];
  65. // [self addSubview:table];
  66. // }
  67. // return self;
  68. //}
  69. - (id)showDropDown:(UIView*)container based:(UIButton *)b height:(CGFloat )height data:(NSDictionary *)json direction:(NSString *)direction current_sel:(long) current_sel {
  70. self.current_sel = current_sel;
  71. btnSender = b;
  72. animationDirection = direction;
  73. self.table = (UITableView *)[super init];
  74. if (self) {
  75. // Initialization code
  76. CGRect btn = b.frame;
  77. // self.list = [NSArray arrayWithArray:arr];
  78. // self.imageList = [NSArray arrayWithArray:imgArr];
  79. self.selector = json;
  80. if ([direction isEqualToString:@"up"]) {
  81. self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
  82. self.layer.shadowOffset = CGSizeMake(-5, -5);
  83. }else if ([direction isEqualToString:@"down"]) {
  84. self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
  85. self.layer.shadowOffset = CGSizeMake(-5, 5);
  86. }
  87. self.layer.masksToBounds = NO;
  88. // self.layer.cornerRadius = 8;
  89. self.layer.shadowRadius = 5;
  90. self.layer.shadowOpacity = 0.5;
  91. table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, btn.size.width, 0)];
  92. table.delegate = self;
  93. table.dataSource = self;
  94. // table.layer.cornerRadius = 5;
  95. table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
  96. table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  97. if ([table respondsToSelector:@selector(setSeparatorInset:)]) {
  98. [table setSeparatorInset:UIEdgeInsetsZero];
  99. }
  100. if ([table respondsToSelector:@selector(setLayoutMargins:)]) {
  101. [table setLayoutMargins:UIEdgeInsetsZero];
  102. }
  103. table.separatorColor = [UIColor grayColor];
  104. __weak typeof(table) weaktable = table;
  105. [UIView animateWithDuration:0.3 animations:^{
  106. if ([direction isEqualToString:@"up"]) {
  107. self.frame = CGRectMake(btn.origin.x, btn.origin.y-height, btn.size.width, height);
  108. } else if([direction isEqualToString:@"down"]) {
  109. self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, height);
  110. }
  111. weaktable.frame = CGRectMake(0, 0, btn.size.width, height);
  112. }];
  113. // [UIView beginAnimations:nil context:nil];
  114. // [UIView setAnimationDuration:0.3];
  115. // [UIView commitAnimations];
  116. self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  117. [container addSubview:self];
  118. [self addSubview:table];
  119. }
  120. return self;
  121. }
  122. -(void)hideDropDown:(UIButton *)b {
  123. CGRect btn = b.frame;
  124. // [UIView beginAnimations:nil context:nil];
  125. // [UIView setAnimationDuration:0.3];
  126. //
  127. // [UIView commitAnimations];
  128. [UIView animateWithDuration:0.3 animations:^{
  129. if ([self->animationDirection isEqualToString:@"up"]) {
  130. self.frame = CGRectMake(btn.origin.x, btn.origin.y, btn.size.width, 0);
  131. }else if ([self->animationDirection isEqualToString:@"down"]) {
  132. self.frame = CGRectMake(btn.origin.x, btn.origin.y+btn.size.height, btn.size.width, 0);
  133. }
  134. self.table.frame = CGRectMake(0, 0, btn.size.width, 0);
  135. } completion:^(BOOL finished) {
  136. [self removeFromSuperview];
  137. }];
  138. }
  139. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  140. return 1;
  141. }
  142. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. return 70;
  144. }
  145. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  146. return [[self.selector valueForKey:@"count"] intValue];
  147. }
  148. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  149. {
  150. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  151. [cell setSeparatorInset:UIEdgeInsetsZero];
  152. }
  153. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  154. [cell setLayoutMargins:UIEdgeInsetsZero];
  155. }
  156. }
  157. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  158. NSString* selectorname = [self.selector valueForKey:@"name"] ;
  159. static NSString *CellIdentifier = @"Cell";
  160. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  161. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  162. if (cell == nil) {
  163. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  164. cell.textLabel.font = [UIFont systemFontOfSize:15];
  165. cell.textLabel.textAlignment = NSTextAlignmentLeft;
  166. }
  167. // NSString *CellIdentifier = @"SelectorCell";
  168. if(self.current_sel==indexPath.row)
  169. {
  170. cell.contentView.layer.borderColor = [UIColor orangeColor].CGColor;
  171. cell.contentView.layer.borderWidth = 1.0;
  172. cell.contentView.layer.cornerRadius=5;
  173. cell.contentView.layer.masksToBounds=true;
  174. }
  175. else
  176. {
  177. cell.contentView.layer.borderWidth = 0.0;
  178. }
  179. cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
  180. NSDictionary* item = [self.selector objectForKey:[NSString stringWithFormat:@"item_%ld",(long)indexPath.row]];
  181. cell.imageView.image=[UIImage imageNamed:@"loading_s"];
  182. cell.textLabel.text=[NSString stringWithFormat:@"%@: %@",selectorname,[item valueForKey:@"title"]];
  183. NSString* img_url = [item valueForKey:@"pic_url"];
  184. NSString* file_name=[img_url lastPathComponent];
  185. NSData* img_data=[iSalesDB load_cached_img:file_name loadFrom:img_url];
  186. if(img_data!=nil)
  187. {
  188. dispatch_async(dispatch_get_main_queue(), ^{
  189. UIImage * img =[UIImage imageWithData:img_data];
  190. cell.imageView.image=img;
  191. });
  192. }
  193. else
  194. {
  195. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  196. NSData* downloadimg_data = [NSData dataWithContentsOfURL:[NSURL URLWithString:img_url]];
  197. dispatch_async(dispatch_get_main_queue(), ^{
  198. if(downloadimg_data!=nil)
  199. {
  200. [iSalesDB cache_img:downloadimg_data filename:file_name saveTo:img_url];
  201. UIImage * img =[UIImage imageWithData:downloadimg_data];
  202. cell.imageView.image=img;
  203. }
  204. else
  205. cell.imageView.image=[UIImage imageNamed:@"notfound_s"];
  206. });
  207. });
  208. }
  209. // cell.selector_valLabel.text =[[section_json objectForKey:@"selector"] valueForKey:@"name"];
  210. // cell.textLabel.textColor = [UIColor whiteColor];
  211. UIView * v = [[UIView alloc] init];
  212. v.backgroundColor = [UIColor grayColor];
  213. cell.selectedBackgroundView = v;
  214. return cell;
  215. }
  216. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  217. [self hideDropDown:btnSender];
  218. if(self.selectChanged)
  219. {
  220. self.selectChanged(indexPath.row);
  221. }
  222. // UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
  223. // [btnSender setTitle:c.textLabel.text forState:UIControlStateNormal];
  224. //
  225. // for (UIView *subview in btnSender.subviews) {
  226. // if ([subview isKindOfClass:[UIImageView class]]) {
  227. // [subview removeFromSuperview];
  228. // }
  229. // }
  230. // imgView.image = c.imageView.image;
  231. // imgView = [[UIImageView alloc] initWithImage:c.imageView.image];
  232. // imgView.frame = CGRectMake(5, 5, 25, 25);
  233. // [btnSender addSubview:imgView];
  234. // [self myDelegate];
  235. }
  236. - (void) myDelegate {
  237. [self.delegate niDropDownDelegateMethod:self];
  238. }
  239. -(void)dealloc {
  240. // [super dealloc];
  241. // [table release];
  242. // [self release];
  243. }
  244. #pragma mark - Touch
  245. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
  246. BOOL inside = [super pointInside:point withEvent:event];
  247. CGPoint btnPoint = [self convertPoint:point toView:btnSender];
  248. BOOL btnInside = [btnSender pointInside:btnPoint withEvent:event];
  249. if (!inside && !btnInside) {
  250. if (self.dropDownTouchOutsideBlk) {
  251. self.dropDownTouchOutsideBlk();
  252. }
  253. }
  254. return inside;
  255. }
  256. @end