iphone4实现下拉框-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 1235605
  • 博文数量: 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。

用起来的确简单。

阅读(3193) | 评论(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

|
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图