// // WTListButton.m // me_watch // // Created by yuklng on 2017/1/20. // Copyright © 2017年 yuklng. All rights reserved. // #import "WTListButton.h" #import "UIView+Extension.h" #import "WTListButtonCell.h" @interface WTListButton() @end @implementation WTListButton{ UITableView* choose_table; UIImageView* xialaImg; BOOL isShow; } #pragma mark 初始化 - (instancetype)init { self = [self initWithList:@[] frame:CGRectMake(0, 0, 0, 0)]; return self; } -(instancetype)initWithFrame:(CGRect)frame{ return [self initWithList:@[] frame:frame]; } -(instancetype)initWithList:(NSArray *)text_list frame:(CGRect)frame{ self =[super initWithFrame:frame]; if (self) { _text_list =[[NSArray alloc] initWithArray:text_list]; self.clickOnClose =YES; isShow =NO; self.curIndex =0; } return self; } // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { [self setTable]; if (choose_table) { [choose_table reloadData]; } } #pragma mark 设置table -(void)setTable{ if (!choose_table) { choose_table =[[UITableView alloc] initWithFrame:self.bounds]; choose_table.x =self.frame.origin.x; choose_table.y =self.getAbsoluteY + 20; choose_table.width =self.width; choose_table.height =self.height*[_text_list count]; choose_table.delegate =self; choose_table.dataSource =self; choose_table.layer.borderColor =[UIColor grayColor].CGColor; choose_table.layer.borderWidth =0.2; choose_table.layer.cornerRadius =5.0; choose_table.layer.masksToBounds=YES; choose_table.separatorStyle = NO; } } #pragma mark UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_text_list count]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return self.height; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *cellIdentifier = @"cell"; WTListButtonCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[WTListButtonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; cell.textLabel.textColor =[UIColor blackColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIFont* newFont =[UIFont preferredFontForTextStyle:UIFontTextStyleBody]; UIFontDescriptor* ctfFont =newFont.fontDescriptor; NSNumber* fontString =[ctfFont objectForKey:@"NSFontSizeAttribute"]; cell.textLabel.font =[UIFont systemFontOfSize:fontString.floatValue-1]; cell.textLabel.textAlignment = NSTextAlignmentCenter; } cell.textLabel.text =_text_list[indexPath.row]; if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } return cell; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row ==self.curIndex) { [cell setSelected:YES animated:NO]; } } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self hideAnimation]; if (self.block && indexPath.row != self.curIndex) { self.curIndex = indexPath.row; [self setTitle:_text_list[indexPath.row] forState:UIControlStateNormal]; self.block(self, indexPath.row); } } #pragma mark view_action - (void)show{ if (_text_list.count > 1) { // isShow = YES; NSIndexPath* curIndexPath =[NSIndexPath indexPathForRow:0 inSection:self.curIndex]; [choose_table selectRowAtIndexPath:curIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; [[self topView] addSubview:choose_table]; // NSIndexPath* curIndexPath =[NSIndexPath indexPathForRow:0 inSection:self.curIndex]; //// [choose_table selectRowAtIndexPath:curIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; // [[choose_table cellForRowAtIndexPath:curIndexPath] setSelected:YES animated:YES]; // [choose_table reloadData]; if ([choose_table respondsToSelector:@selector(setSeparatorInset:)]) { [choose_table setSeparatorInset:UIEdgeInsetsZero]; } if ([choose_table respondsToSelector:@selector(setLayoutMargins:)]) { [choose_table setLayoutMargins:UIEdgeInsetsZero]; } if (choose_table) { [choose_table reloadData]; } } } - (void)show:(UIViewController*)vc{ if (_text_list.count > 1) { [vc.view addSubview:choose_table]; if ([choose_table respondsToSelector:@selector(setSeparatorInset:)]) { [choose_table setSeparatorInset:UIEdgeInsetsZero]; } if ([choose_table respondsToSelector:@selector(setLayoutMargins:)]) { [choose_table setLayoutMargins:UIEdgeInsetsZero]; } if (choose_table) { [choose_table reloadData]; } } } //去除该组件 - (void)hideAnimation{ isShow = NO; [choose_table removeFromSuperview]; } -(void)setText_list:(NSArray *)text_list{ _text_list =[[NSArray alloc] initWithArray:text_list]; if (choose_table) { choose_table.height =self.height*[_text_list count]; [choose_table reloadData]; } } -(void)setText_list:(NSArray *)text_list block:(BlockListBtnClick)block{ self.curIndex = [text_list indexOfObject:self.titleLabel.text]; [self setText_list:text_list]; if (!xialaImg) { xialaImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiala"]]; [self addSubview:xialaImg]; // xialaImg.size // xialaImg.x = self.width - 30; // xialaImg.centerY = self.centerY; } if (text_list.count > 1) { xialaImg.hidden = NO; }else{ xialaImg.hidden = YES; } // //交换文字和图片的位置 // [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.imageView.image.size.width, // 0, self.imageView.image.size.width)]; // [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.frame.size.width + 5, // 0, -self.titleLabel.frame.size.width)]; if (block) { self.block = block; } [self addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchDown]; } - (void)action{ if (isShow) { [self hideAnimation]; }else{ isShow = YES; [self show]; } } //获取根视图 -(UIView*)topView{ UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; return rootViewController.view; } @end