iphone开发之自定义UICombox

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

说明:转载请注明出处!

你可能感兴趣的:(iphone开发之自定义UICombox)