代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
ObjC
】
IOS自定义ComboBox
作者:
Android.cc
/ 发布于
2013/11/28
/
1107
自定义toggleView、comboBox
/* 头文件 */ #import <UIKit/UIKit.h> #import "CCButtonDelegate.h" #define ROW_HEIGHT 30 #define ROWLINE 6 #define ROWWIDTH 280 @protocol CCComboBoxDelegate; @interface CCComboBox : UIView<UITableViewDataSource, UITableViewDelegate, CCButtonDelegate> { @private UIButton *_button; UITableView *_tableView; NSInteger _currentRow; NSUInteger _rowMaxLines; BOOL isHidden; UIView *_overlayView; } @property (assign, nonatomic) id<CCComboBoxDelegate> delegate; @property (strong, nonatomic, setter = setDataArray:) NSMutableArray *dataArray; - (id)initWithFrame:(CGRect)frame; - (void)setDataArray:(NSMutableArray *)dataArray; @end @interface CCComboBox (UIButton) - (void)setComboBoxBackgroundImage:(UIImage *)image forState:(UIControlState)state; - (void)setComboBoxFrame:(CGRect)frame; - (id)getComboBoxSelectedItem; @end @interface CCComboBox (UITableView) - (void)setComboBoxShowMaxLine:(NSUInteger)count; @end /*具体实现*/ #import "CCComboBox.h" #import "UIButton+CCButton.h" #import "CCComboBoxDelegate.h" @interface CCComboBox (Private) - (void)showOrHidden:(BOOL)hidden; - (void)fadeIn; - (void)fadeOut; @end @implementation CCComboBox - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.frame = frame; _currentRow = 0; _dataArray = [NSMutableArray arrayWithCapacity:0]; isHidden = YES; _button = [[UIButton alloc] initWithNormalImage:nil andHighLighted:nil frame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; _button.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, frame.size.width/18.0); _button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [_button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:_button]; } return self; } - (void) setDataArray:(NSMutableArray *)dataArray { _dataArray = dataArray; if (_dataArray.count > 0) { if (_delegate) { [_delegate selected:self atIndex:0]; } [_button setTitle:[_dataArray objectAtIndex:0] forState:UIControlStateNormal]; } if (_tableView) { [_tableView reloadData]; } } #pragma mark - UIButton - (void)setComboBoxBackgroundImage:(UIImage *)image forState:(UIControlState)state { [_button setBackgroundImage:image forState:state]; } - (void)setComboBoxFrame:(CGRect)frame { self.frame = frame; _button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); } - (id)getComboBoxSelectedItem { return [_dataArray objectAtIndex:_currentRow]; } #pragma mark - UITableView - (void)setComboBoxShowMaxLine:(NSUInteger)count { _rowMaxLines = count; if (_tableView) { _tableView.frame = CGRectMake(0, 0, ROWWIDTH, _dataArray.count > _rowMaxLines ? ROW_HEIGHT *_rowMaxLines : ROW_HEIGHT * _dataArray.count); } } #pragma mark - UIButtonDelegate - (void)clickAction:(id)sender { if (!_tableView) { _rowMaxLines = _rowMaxLines == 0 ? ROWLINE : _rowMaxLines; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ROWWIDTH, _dataArray.count > _rowMaxLines ? ROW_HEIGHT * _rowMaxLines : ROW_HEIGHT * _dataArray.count) style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.layer.borderWidth = 1; _tableView.layer.borderColor = [[UIColor grayColor] CGColor]; _tableView.layer.cornerRadius = 5; _overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5]; } [self showOrHidden:(isHidden = !isHidden)]; } #pragma mark - UITableViewDataDelegate - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [_dataArray objectAtIndex:[indexPath row]]; cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14]; return cell; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataArray.count; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [_button setTitle:[_dataArray objectAtIndex:[indexPath row]] forState:UIControlStateNormal]; if (_delegate && _currentRow != [indexPath row]) { [_delegate selected:self atIndex:[indexPath row]]; } _currentRow = [indexPath row]; [self showOrHidden:(isHidden = !isHidden)]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return ROW_HEIGHT; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath row] % 1 == 0) { //cell.backgroundColor = [UIColor grayColor]; } } #pragma mark - private - (void)showOrHidden:(BOOL)hidden { if (hidden) { [self fadeOut]; }else { UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow]; [keywindow addSubview:_overlayView]; [keywindow addSubview:_tableView]; _tableView.center = CGPointMake(keywindow.bounds.size.width/2.0f, keywindow.bounds.size.height/2.0f); [self fadeIn]; } } - (void)fadeIn { _tableView.transform = CGAffineTransformMakeScale(1.3, 1.3); _tableView.alpha = 0; [UIView animateWithDuration:.35 animations:^{ _tableView.alpha = 1; _tableView.transform = CGAffineTransformMakeScale(1, 1); }]; } - (void)fadeOut { [UIView animateWithDuration:.15 animations:^{ _tableView.transform = CGAffineTransformMakeScale(1.3, 1.3); _tableView.alpha = 0.0; } completion:^(BOOL finished) { if (finished) { [_overlayView removeFromSuperview]; [_tableView removeFromSuperview]; } }]; } @end /*应用*/ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; CCComboBox *comboBox = [[CCComboBox alloc] initWithFrame:CGRectMake(10, 10, 300, 30)]; comboBox.dataArray = _dataArray; [comboBox setComboBoxBackgroundImage:[UIImage imageNamed:@"select_button_bg"] forState:UIControlStateNormal]; comboBox.tag = 10000; //[self.window addSubview:comboBox]; comboBox.delegate = self; NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"--全部片区--", @"武侯区", @"青阳社区", @"金牛社区", @"成华区", @"温江区", @"温江1区", @"龙泉驿区", @"龙泉2区"]]; //NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0]; [comboBox setDataArray:array]; UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, comboBox.frame.size.height +20)]; [subView addSubview:comboBox]; subView.backgroundColor = [UIColor grayColor]; CCToggleView *toggleView = [[CCToggleView alloc] initWithSubView:subView subViewVisiable:NO]; [toggleView setToggleBackgroundImage:[UIImage imageNamed:@"toggle_normal"] forState:UIControlStateNormal]; [toggleView setToggleBackgroundImage:[UIImage imageNamed:@"toggle_selected"] forState:UIControlStateSelected]; [self.window addSubview:toggleView]; [self.window makeKeyAndVisible]; return YES; }
试试其它关键字
ComboBox
同语言下
.
根据生日计算星座
.
精简的实现一个内存池
.
iOS索引搜索核心代码
.
iOS 搜索框
.
文件操作
.
UIWebView 加载本地网页
.
取消导航条对视图的影响
.
利用终端显示 隐藏文件
.
更改导航条背景颜色
.
iOS 字体斜体
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Android.cc
贡献的其它代码
(
2
)
.
解析任意层数Json,将Map、List对象封装为Json
.
IOS自定义ComboBox
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3