快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

线上葡京娱乐场3730:【iPhone开发教程】实现自定义下拉组件UICombox



因为ios 中没有下拉组件,下面给大年夜家分享一个实现下拉组件的代码:

//

// CloCombox.h // ColCombox

// // Created by cloay on 12-11-12.

// Copyright (c) 2012年 topgether. All rights reserved. //

#import

@protocol CloComboxDelegate;

@interface CloCombox : UIView {UIButton *title;

UITableView *cloTable; }

@property (nonatomic, retain) iddelegate; @property (nonatomic, retain) NSArray *tableItems;

- (线上葡京娱乐场3730void)setTitle:(NSString *)titleStr; - (id)initWithFrame:(CGRect)frame items:(NSArray *)items inView:(UIView *)view;

@end

@protocol CloComboxDelegate

- (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:(CG线上葡京娱乐场3730Rect)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];

[titl线上葡京娱乐场3730e 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; }

#prag线上葡京娱乐场3730ma mark -

#pragma mark tableview delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{[tableView deselectRowAtIndexPath:indexPath animated:YES];

[self setTitle:[tableItems objectA线上葡京娱乐场3730tIndex:indexPath.row]];tableView.hidden = YES;

if ([delegate respondsToSelector:@selector(itemDidSelected:)]) {[delegate itemDidSelected:indexPath.row];

} }

@end

demo下载:CloCombox

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: