iPhone4实现下拉框

3556阅读 1评论2010-11-24 sxcong
分类:

参考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。

用起来的确简单。

上一篇:live555 media server文件播放与读内存播放
下一篇:完整的SIP软电话工程实施过程

文章评论