Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1239661
  • 博文数量: 76
  • 博客积分: 1959
  • 博客等级: 上尉
  • 技术积分: 2689
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 12:07
个人简介

樽中酒不空

文章分类

全部博文(76)

文章存档

2020年(4)

2019年(1)

2017年(2)

2016年(2)

2015年(7)

2014年(11)

2013年(13)

2012年(18)

2011年(2)

2010年(16)

分类:

2010-11-24 17:06:56

参考http://blog.csdn.net/kmyhy/archive/2010/09/10/5876009.aspx
这上面实现的。
不过这个排版也太差了,变量都连到了一起。另外,程序也不完整。参考着这个方法做了一下,代码贴上:

//DropDownList.h
#1 import

@protocol DropDownListDelegate

@required
//-(void)selected:(NSString*)k displayLabel:(NSString*)v;
-(void)rsp:(NSString*)value;
@end

@interface DropDownList : UIView
{
    UITextField     *textField;
    UITableView        *listView;
    NSArray            *list;
    BOOL            showList;
    CGRect oldFrame, newFrame;
    UIColor *lineColor, *listBgolor;
    CGFloat lineWidth;
    UITextBorderStyle borderStyle;
    iddelegate;
}

@property (nonatomic, retain) UITextField*    textField;
@property (nonatomic, retain) NSArray            *list;
@property (nonatomic, retain) UITableView        *listView;
@property (nonatomic, retain) UIColor *lineColor, *listBgolor;
@property (nonatomic, assign) UITextBorderStyle borderStyle;
@property(nonatomic, assign)iddelegate;

-(void)drawView;
-(void)dropdown;
-(void)setShowList:(BOOL)b;

@end

//2 DropDownList.mm
#import "DropDownList.h"


@implementation DropDownList

@synthesize textField;
@synthesize list;
@synthesize listView;
@synthesize lineColor;
@synthesize listBgolor;
@synthesize borderStyle;
@synthesize delegate;


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        list = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
        borderStyle = UITextBorderStyleRoundedRect;
       
        showList = NO;
        oldFrame = frame;
        newFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height*5);
       
        lineColor = [UIColor lightGrayColor];
        listBgolor = [UIColor whiteColor];
        lineWidth = 1;
       
        self.backgroundColor = [UIColor clearColor];
        [self drawView];
    }
    return self;
}

-(void)dropdown
{
    [textField resignFirstResponder];
    if (showList) {
        return;
    }
    else {
        [self.superview bringSubviewToFront:self];
        [self setShowList:YES];
    }

}

- (void)drawView
{
    textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, oldFrame.size.width, oldFrame.size.height)];
    textField.borderStyle = borderStyle;
    [self addSubview:textField];
    [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllEvents];
   
    listView = [[UITableView alloc]initWithFrame:CGRectMake(lineWidth, oldFrame.size.height+lineWidth, oldFrame.size.width-lineWidth*2, oldFrame.size.height*4-lineWidth*2)];
    listView.dataSource = self;
    listView.delegate = self;
    listView.backgroundColor = listBgolor;
    listView.separatorColor = lineColor;
    listView.hidden = !showList;
    [self addSubview:listView];
    [listView release];
}


#pragma mark -
#pragma mark UITableViewDelegate and UITableViewDatasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ListCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = (NSString *)[list objectAtIndex:indexPath.row];
    //cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
    //cell.accessoryType = UITableViewCellAccessoryNone;
    //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = textField.font;
   
    return cell;
}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return oldFrame.size.height;
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    textField.text = (NSString*)[list objectAtIndex:indexPath.row];
    [self setShowList:NO];
    if (delegate != nil)
    {
        NSString* data1 = @"key";//演示用,正常应该是textField.text
        [delegate rsp:data1];

        //[delegate performSelector:@selector(selected:displayLabel:)withObject:data withObject:textField.text];

    }
}


-(BOOL)showlist
{
    return showList;
}
-(void)setShowList:(BOOL)b
{
    showList = b;
    if (b)
    {
        self.frame = newFrame;
    }
    else
    {
        self.frame = oldFrame;
    }
    listView.hidden = !b;
    [self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect drawRect;
    if (showList)
    {
        CGContextSetStrokeColorWithColor(ctx, [lineColor CGColor]);
        drawRect = listView.frame;
        CGContextStrokeRect(ctx, drawRect);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
    [super dealloc];
}


@end


//3 testController.h
//在view controller中调用
#import "DropDownList.h"

//注意红字部分
@interface testController : UIViewController

//4 testController.mm
#import "DropDownList.h"
- (void)viewDidLoad
{
    DropDownList* tf = [[DropDownList alloc]initWithFrame:CGRectMake(144,73,140,30)];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    tf.delegate = self;
    [self.view addSubview:tf];
    [tf release];
}

-(void)rsp:(NSString*)value
{
    NSLog(@"%@",value);
}

//到此结束
////////////////////////////////

只是演示怎么用,只是简单写了几个数,没有用NSDictionary。

用起来的确简单。

阅读(3238) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

sxcong2010-11-25 10:27:11

上面实现的下拉框还有几个小问题: 1 点击窗口弹出下拉框,但在同一位置再点,这个下拉框不消失。 2 在此基础上要对选中的进行编辑 3 初始化赋值。 解决: 1 在drawView中,textField addTarget:self这里,后面的参数原来是 UIControlEventAllEvents,修改成UIControlEventTouchDown|UICopntrolEventAllEditingEvents 然后,在dropdown里, if (showList) { //新增: [self setShowList:NO]; } 2 实现可编译的下拉框,只需要把textField addTarget:self的参数改成UIControlEventTouchDown。然后,返回值那里注意,以textFiled为准。 3 增加setlist 函数,设置数组进去。不过有个问题,对textField赋值无效。只好修改drawView,带一个参数。最初drawView是由initWithFrame来调用,现在修改一下,由外面生成dropdownli