MyDrop.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // MyDrop.m
  3. // test1107
  4. //
  5. // Created by apple on 15/11/10.
  6. // Copyright (c) 2015年 JCZ. All rights reserved.
  7. //
  8. #import "MyDrop.h"
  9. #import "TRButton.h"
  10. @interface MyDrop()<UITableViewDataSource,UITableViewDelegate>
  11. @property(nonatomic,strong)TRButton* titleBtn;
  12. @property(nonatomic,strong)UITableView* table;
  13. @property(nonatomic,strong)UITableView* table2;
  14. @property(nonatomic,weak)NSArray* subArr;
  15. @property(nonatomic,assign) int indexRow;
  16. @end
  17. CGRect allFrame,oriFrame;
  18. NSString* firTitle;
  19. @implementation MyDrop
  20. -(instancetype)initWithFrame:(CGRect)frame{
  21. self = [super initWithFrame:frame];
  22. oriFrame = frame;
  23. if (self) {
  24. _titleBtn = [[TRButton alloc] initWithFrame:self.bounds];
  25. [_titleBtn setTitle:@"沈阳市" forState:UIControlStateNormal];
  26. [_titleBtn addTarget:self action:@selector(drop) forControlEvents:UIControlEventTouchUpInside];
  27. [self addSubview:_titleBtn];
  28. _cellH = frame.size.height;
  29. _cellW = frame.size.width * 1.5;
  30. _table = [[UITableView alloc] init];
  31. _table.hidden = YES;
  32. _table.tag = 1;
  33. // _table.scrollEnabled = NO;
  34. _table.separatorColor = [UIColor whiteColor];
  35. [self addSubview:_table];
  36. _table2 = [[UITableView alloc] init];
  37. _table2.delegate = self;
  38. _table2.tag = 2;
  39. // _table2.scrollEnabled = NO;
  40. _table2.dataSource = self;
  41. _table2.hidden = YES;
  42. _table2.separatorColor = [UIColor whiteColor];
  43. [self addSubview:_table2];
  44. CGSize mySize = [[UIScreen mainScreen] bounds].size;
  45. allFrame = CGRectMake(frame.origin.x, frame.origin.y, mySize.width, mySize.height);
  46. }
  47. return self;
  48. }
  49. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  50. {
  51. UITouch* touch = [touches anyObject];
  52. CGPoint pos = [touch locationInView:self];
  53. if (CGRectContainsPoint(_table.frame, pos) || CGRectContainsPoint(_table2.frame, pos)) {
  54. return;
  55. }
  56. _table.hidden = YES;
  57. _table2.hidden = YES;
  58. self.frame = oriFrame;
  59. }
  60. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  61. {
  62. return _cellH;
  63. }
  64. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  65. // [tableView deselectRowAtIndexPath:indexPath animated:YES];
  66. if (1 == tableView.tag) {
  67. if (indexPath.row < _firArr.count) {
  68. _indexRow = (int)indexPath.row;
  69. firTitle = _firArr[indexPath.row ];
  70. _subArr = _secArr [indexPath.row];
  71. _table2.frame = CGRectMake(_cellW, _cellH, _cellW, _cellH *(1 + _subArr.count));
  72. [_table2 reloadData];
  73. _table2.hidden = NO;
  74. }else{
  75. _subArr = nil;
  76. }
  77. return;
  78. }
  79. if (2 == tableView.tag) {
  80. [self setPlaceHolder:_subArr[indexPath.row]];
  81. [self hideTableView:_table];
  82. [self hideTableView:_table2];
  83. [_titleBtn makeAnimate];
  84. [self.delegate didGetResult1:firTitle andResult2:_subArr[indexPath.row]];
  85. }
  86. }
  87. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  88. {
  89. if (1 == tableView.tag) {
  90. if (_firArr) {
  91. return _firArr.count + 2;
  92. }
  93. return 0;
  94. }
  95. if (2 == tableView.tag) {
  96. if (_subArr) {
  97. return _subArr.count + 2;
  98. }
  99. return 0;
  100. }
  101. return 0;
  102. }
  103. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  104. {
  105. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _table.width, 40)];
  106. btn.tag = tableView.tag;
  107. [btn setTitleColor:kTitleColor forState:UIControlStateNormal];
  108. if (1 == tableView.tag) {
  109. [btn setTitle:@"全省" forState:UIControlStateNormal];
  110. }
  111. if (2 == tableView.tag) {
  112. [btn setTitle:@"全市" forState:UIControlStateNormal];
  113. }
  114. btn.backgroundColor = backGroundColor;
  115. [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
  116. return btn;
  117. }
  118. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  119. {
  120. return 40;
  121. }
  122. -(void)click:(UIButton *)btn
  123. {
  124. [self hideTableView:_table];
  125. [self hideTableView:_table2];
  126. [_titleBtn makeAnimate];
  127. NSString *str;
  128. if (btn.tag == 1)
  129. {
  130. str = @"全省";
  131. [self.delegate didGetResult1:@"查不到" andResult2:@"查不到"];
  132. }
  133. else
  134. {
  135. str = _firArr[_indexRow];
  136. [self.delegate didGetResult1:_firArr[_indexRow] andResult2:@"查不到"];
  137. }
  138. [self setPlaceHolder:str];
  139. }
  140. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. if (1 == tableView.tag) {
  143. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@""];
  144. if (!cell) {
  145. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
  146. }
  147. if (indexPath.row < _firArr.count) {
  148. //越界就不赋值了。因为有2个填充cell。所以预防越界。
  149. cell.textLabel.text = _firArr[indexPath.row];
  150. }else{
  151. cell.textLabel.text = nil;
  152. }
  153. return cell;
  154. }
  155. if (2 == tableView.tag) {
  156. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
  157. if (!cell) {
  158. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"c"];
  159. }
  160. if (indexPath.row < _subArr.count) {
  161. cell.textLabel.text = _subArr[indexPath.row];
  162. }else{
  163. cell.textLabel.text = nil;
  164. }
  165. return cell;
  166. }
  167. UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qiguaideID"];
  168. return cell;
  169. }
  170. -(void)setPlaceHolder:(NSString*)str{
  171. [_titleBtn setTitle:str forState:UIControlStateNormal];
  172. }
  173. -(void)drop
  174. {
  175. [_titleBtn makeAnimate];
  176. //把dropdownList放到前面,防止下拉框被别的控件遮住
  177. [self.superview bringSubviewToFront:self];
  178. if (_table.hidden) {
  179. _table.hidden = NO;
  180. [self showTableView:_table];
  181. self.frame = allFrame;
  182. }else{
  183. self.frame = oriFrame;
  184. // NSLog(@"up");
  185. [self hideTableView:_table];
  186. [self hideTableView:_table2];
  187. // _table.hidden = YES;
  188. // _table2.hidden = YES;
  189. }
  190. }
  191. -(void)hideTableView:(UITableView* )table{
  192. CGRect frame = table.frame;
  193. CGRect frame2 = table.frame;
  194. frame.size.height = 0;
  195. [UIView animateWithDuration:.2 animations:^{
  196. table.frame = frame;
  197. } completion:^(BOOL finished) {
  198. table.hidden = YES;
  199. table.frame = frame2;
  200. if (2==table.tag) {
  201. self.frame = oriFrame;
  202. }
  203. }];
  204. }
  205. -(void)showTableView:(UITableView* )table{
  206. table.hidden = NO;
  207. [table reloadData];
  208. CGRect frame = table.frame;
  209. CGRect frame2 = table.frame;
  210. frame.size.height = 0;
  211. table.frame = frame;
  212. [UIView animateWithDuration:.2 animations:^{
  213. table.frame = frame2;
  214. } completion:^(BOOL finished) {
  215. table.hidden = NO;
  216. }];
  217. }
  218. -(void)setFirArr:(NSArray *)firArr
  219. {
  220. _firArr = firArr;
  221. _table.frame = CGRectMake(0, _cellH, _cellW, _cellH* (firArr.count+1));
  222. _table.delegate = self;
  223. _table.dataSource = self;
  224. [_table reloadData];
  225. }
  226. -(void)setSecArr:(NSArray *)secArr{
  227. _secArr = secArr;
  228. if (_firArr.count > _secArr.count) {
  229. //NSLog(@"ERROE:fir.cnt=%d sec.cnt=%d",(int)_firArr.count,(int)_secArr.count);
  230. }
  231. _table2.frame = CGRectMake(0, _cellH, _cellW, _cellH* (secArr.count+1));
  232. _table2.delegate = self;
  233. _table2.dataSource = self;
  234. [_table2 reloadData];
  235. }
  236. @end