123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- //
- // MyDrop.m
- // test1107
- //
- // Created by apple on 15/11/10.
- // Copyright (c) 2015年 JCZ. All rights reserved.
- //
- #import "MyDrop.h"
- #import "TRButton.h"
- @interface MyDrop()<UITableViewDataSource,UITableViewDelegate>
- @property(nonatomic,strong)TRButton* titleBtn;
- @property(nonatomic,strong)UITableView* table;
- @property(nonatomic,strong)UITableView* table2;
- @property(nonatomic,weak)NSArray* subArr;
- @property(nonatomic,assign) int indexRow;
- @end
- CGRect allFrame,oriFrame;
- NSString* firTitle;
- @implementation MyDrop
- -(instancetype)initWithFrame:(CGRect)frame{
- self = [super initWithFrame:frame];
- oriFrame = frame;
- if (self) {
- _titleBtn = [[TRButton alloc] initWithFrame:self.bounds];
- [_titleBtn setTitle:@"沈阳市" forState:UIControlStateNormal];
- [_titleBtn addTarget:self action:@selector(drop) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_titleBtn];
- _cellH = frame.size.height;
- _cellW = frame.size.width * 1.5;
-
- _table = [[UITableView alloc] init];
- _table.hidden = YES;
- _table.tag = 1;
- // _table.scrollEnabled = NO;
- _table.separatorColor = [UIColor whiteColor];
- [self addSubview:_table];
- _table2 = [[UITableView alloc] init];
- _table2.delegate = self;
- _table2.tag = 2;
- // _table2.scrollEnabled = NO;
- _table2.dataSource = self;
- _table2.hidden = YES;
- _table2.separatorColor = [UIColor whiteColor];
- [self addSubview:_table2];
-
- CGSize mySize = [[UIScreen mainScreen] bounds].size;
- allFrame = CGRectMake(frame.origin.x, frame.origin.y, mySize.width, mySize.height);
- }
-
- return self;
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch* touch = [touches anyObject];
- CGPoint pos = [touch locationInView:self];
- if (CGRectContainsPoint(_table.frame, pos) || CGRectContainsPoint(_table2.frame, pos)) {
- return;
- }
- _table.hidden = YES;
- _table2.hidden = YES;
- self.frame = oriFrame;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return _cellH;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- // [tableView deselectRowAtIndexPath:indexPath animated:YES];
- if (1 == tableView.tag) {
- if (indexPath.row < _firArr.count) {
-
- _indexRow = (int)indexPath.row;
-
- firTitle = _firArr[indexPath.row ];
- _subArr = _secArr [indexPath.row];
- _table2.frame = CGRectMake(_cellW, _cellH, _cellW, _cellH *(1 + _subArr.count));
- [_table2 reloadData];
- _table2.hidden = NO;
- }else{
- _subArr = nil;
- }
- return;
- }
- if (2 == tableView.tag) {
- [self setPlaceHolder:_subArr[indexPath.row]];
- [self hideTableView:_table];
- [self hideTableView:_table2];
- [_titleBtn makeAnimate];
- [self.delegate didGetResult1:firTitle andResult2:_subArr[indexPath.row]];
- }
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- if (1 == tableView.tag) {
- if (_firArr) {
- return _firArr.count + 2;
- }
- return 0;
- }
- if (2 == tableView.tag) {
- if (_subArr) {
- return _subArr.count + 2;
- }
- return 0;
- }
- return 0;
- }
- -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _table.width, 40)];
- btn.tag = tableView.tag;
- [btn setTitleColor:kTitleColor forState:UIControlStateNormal];
-
- if (1 == tableView.tag) {
- [btn setTitle:@"全省" forState:UIControlStateNormal];
- }
- if (2 == tableView.tag) {
- [btn setTitle:@"全市" forState:UIControlStateNormal];
- }
- btn.backgroundColor = backGroundColor;
- [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
- return btn;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- return 40;
- }
- -(void)click:(UIButton *)btn
- {
- [self hideTableView:_table];
- [self hideTableView:_table2];
- [_titleBtn makeAnimate];
- NSString *str;
- if (btn.tag == 1)
- {
- str = @"全省";
- [self.delegate didGetResult1:@"查不到" andResult2:@"查不到"];
- }
- else
- {
- str = _firArr[_indexRow];
- [self.delegate didGetResult1:_firArr[_indexRow] andResult2:@"查不到"];
- }
- [self setPlaceHolder:str];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (1 == tableView.tag) {
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@""];
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
- }
- if (indexPath.row < _firArr.count) {
- //越界就不赋值了。因为有2个填充cell。所以预防越界。
- cell.textLabel.text = _firArr[indexPath.row];
- }else{
- cell.textLabel.text = nil;
- }
-
- return cell;
- }
- if (2 == tableView.tag) {
- UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"c"];
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"c"];
- }
- if (indexPath.row < _subArr.count) {
- cell.textLabel.text = _subArr[indexPath.row];
- }else{
- cell.textLabel.text = nil;
- }
- return cell;
- }
-
- UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qiguaideID"];
- return cell;
- }
- -(void)setPlaceHolder:(NSString*)str{
- [_titleBtn setTitle:str forState:UIControlStateNormal];
- }
- -(void)drop
- {
- [_titleBtn makeAnimate];
- //把dropdownList放到前面,防止下拉框被别的控件遮住
- [self.superview bringSubviewToFront:self];
- if (_table.hidden) {
- _table.hidden = NO;
- [self showTableView:_table];
- self.frame = allFrame;
- }else{
- self.frame = oriFrame;
- // NSLog(@"up");
- [self hideTableView:_table];
- [self hideTableView:_table2];
- // _table.hidden = YES;
- // _table2.hidden = YES;
- }
- }
- -(void)hideTableView:(UITableView* )table{
- CGRect frame = table.frame;
- CGRect frame2 = table.frame;
- frame.size.height = 0;
- [UIView animateWithDuration:.2 animations:^{
- table.frame = frame;
- } completion:^(BOOL finished) {
- table.hidden = YES;
- table.frame = frame2;
- if (2==table.tag) {
- self.frame = oriFrame;
- }
- }];
- }
- -(void)showTableView:(UITableView* )table{
- table.hidden = NO;
- [table reloadData];
- CGRect frame = table.frame;
- CGRect frame2 = table.frame;
- frame.size.height = 0;
- table.frame = frame;
- [UIView animateWithDuration:.2 animations:^{
- table.frame = frame2;
- } completion:^(BOOL finished) {
- table.hidden = NO;
- }];
-
- }
- -(void)setFirArr:(NSArray *)firArr
- {
- _firArr = firArr;
- _table.frame = CGRectMake(0, _cellH, _cellW, _cellH* (firArr.count+1));
- _table.delegate = self;
- _table.dataSource = self;
- [_table reloadData];
- }
- -(void)setSecArr:(NSArray *)secArr{
- _secArr = secArr;
- if (_firArr.count > _secArr.count) {
- //NSLog(@"ERROE:fir.cnt=%d sec.cnt=%d",(int)_firArr.count,(int)_secArr.count);
- }
- _table2.frame = CGRectMake(0, _cellH, _cellW, _cellH* (secArr.count+1));
- _table2.delegate = self;
- _table2.dataSource = self;
- [_table2 reloadData];
- }
- @end
|