参考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。
用起来的确简单。
阅读(3193) | 评论(1) | 转发(0) |