iOS 中沒有下拉組件,下面是自己實現的分享給大家!
- //
- // CloCombox.h
- // ColCombox
- //
- // Created by cloay on 12-11-12.
- // Copyright (c) 2012年 topgether. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @protocol CloComboxDelegate;
- @interface CloCombox : UIView <UITableViewDataSource, UITableViewDelegate>{
- UIButton *title;
- UITableView *cloTable;
- }
- @property (nonatomic, retain) id <CloComboxDelegate> delegate;
- @property (nonatomic, retain) NSArray *tableItems;
- - (void)setTitle:(NSString *)titleStr;
- - (id)initWithFrame:(CGRect)frame items:(NSArray *)items inView:(UIView *)view;
- @end
- @protocol CloComboxDelegate <NSObject>
- - (void)itemDidSelected:(NSInteger)index;
- @end
實現:
- //
- // CloCombox.m
- // ColCombox
- //
- // Created by cloay on 12-11-12.
- // Copyright (c) 2012年 topgether. All rights reserved.
- //
- #import "CloCombox.h"
- @implementation CloCombox
- @synthesize delegate, tableItems;
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (id)initWithFrame:(CGRect)frame items:(NSArray *)items inView:(UIView *)view{
- self = [self initWithFrame:frame];
- self.tableItems = items;
- title = [UIButton buttonWithType:UIButtonTypeCustom];
- [title setFrame:CGRectMake(0, 0, frame.size.width, 25)];
- [title setTitle:@"未選擇" forState:UIControlStateNormal];
- [title setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
- [title addTarget:self action:@selector(titleBtnDidTaped:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:title];
- cloTable = [[UITableView alloc] initWithFrame:CGRectMake((view.frame.size.width - frame.size.width)/2, 0, frame.size.width, 30*items.count) style:UITableViewStylePlain];
- cloTable.delegate = self;
- cloTable.dataSource = self;
- cloTable.hidden = YES;
- [view addSubview:cloTable];
- return self;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- - (void)setTitle:(NSString *)titleStr{
- [title setTitle:titleStr forState:UIControlStateNormal];
- }
- - (IBAction)titleBtnDidTaped:(id)sender{
- cloTable.hidden = !cloTable.hidden;
- }
- - (void)dealloc{
- [super dealloc];
- [title release];
- [cloTable release];
- }
- #pragma mark-
- #pragma table view datasource
- - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 30;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return tableItems.count;
- }
- - (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *identifier = @"CELLIDENTIFIER";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
- //if sdk version is below 6.0 instead by UITextAlignmentCenter
- [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
- }
- cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
- return cell;
- }
- #pragma mark -
- #pragma mark tableview delegate methods
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- [self setTitle:[tableItems objectAtIndex:indexPath.row]];
- tableView.hidden = YES;
- if ([delegate respondsToSelector:@selector(itemDidSelected:)]) {
- [delegate itemDidSelected:indexPath.row];
- }
- }
- @end
DEMO下載:CloCombox
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2012年資料/11月/13日/iPhone開發之自定義UICombox