IOS development _ iPhone development _ How to Implement the drop-down list on the iPhone interface

Source: Internet
Author: User

The Code is as follows:

# Import <uikit/uikit. h>
@ Interface dropdownlist: uiview <uitableviewdelegate, uitableviewdatasource> {
Uitextfield * textfield; // text input box
Nsarray * List; // drop-down list data
Bool showlist; // whether to display the drop-down list
Uitableview * listview; // drop-down list
Cgrect oldframe, newframe; // The rectangle of the entire control (including before and after the drop-down)
Uicolor * linecolor, * listbgcolor; // The border color and background color of the drop-down box
Cgfloat linewidth; // The Border width of the drop-down box.
Uitextborderstyle borderstyle; // text box border Style
}
@ Property (nonatomic, retain) uitextfield * textfield;
@ Property (nonatomic, retain) nsarray * List;
@ Property (nonatomic, retain) uitableview * listview;
@ Property (nonatomic, retain) uicolor * linecolor, * listbgcolor;
@ Property (nonatomic, assign) uitextborderstyle borderstyle;
-(Void) drawview;
-(Void) setshowlist :( bool) B;
@ End
# Import "dropdownlist. H"
@ Implementation dropdownlist
@ Synthesize textfield, list, listview, linecolor, listbgcolor, borderstyle;
-(ID) initwithframe :( cgrect) frame {

If (Self = [Super initwithframe: frame]) {
// Data in the default drop-down list
List = [[nsarray alloc] initwithobjects: @ "1", @ "2", @ "3", @ "4", nil];

Borderstyle = uitextborderstyleroundedrect;

Showlist = no; // the drop-down list is not displayed by default.
Oldframe = frame; // initial size of the widget when no pull-down is performed
// When the drop-down box is displayed, the control size is calculated.
Newframe = cgrectmake (frame. Origin. X, frame. Origin. Y, frame. Size. Width, frame. Size. Height * 5 );

Linecolor = [uicolor lightgraycolor]; // the border line of the default list is gray.
Listbgcolor = [uicolor whitecolor]; // the background color of the default list box is white.
Linewidth = 1; // The Border width of the default list is 1.

// Set the background color to transparent, otherwise there will be a Black edge
Self. backgroundcolor = [uicolor clearcolor];
[Self drawview]; // call a method to draw controls

}
Returnself;
}
-(Void) drawview {
// Text box
Textfield = [[uitextfield alloc]
Initwithframe: cgrectmake (0, 0,
Oldframe. Size. Width,
Oldframe. Size. Height)];
Textfield. borderstyle = borderstyle; // you can specify the border style of the text box.
[Self addsubview: textfield];
[Textfield addtarget: Self action: @ selector (dropdown) forcontrolevents: uicontroleventalltouchevents];

// Drop-down list
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 = listbgcolor;
Listview. separatorcolor = linecolor;
Listview. Hidden =! Showlist; // The listview is hidden at the beginning, and then displayed or hidden based on the value of showlist.

[Self addsubview: listview];
[Listview release];
}
-(Void) dropdown {
[Textfield resignfirstresponder];
If (showlist) {// If the drop-down box is displayed, nothing will be done
Return;
} Else {// If the drop-down list is not displayed
// Put the dropdownlist in front to prevent the drop-down box from being hidden by other controls

[Self. superview bringsubviewtofront: Self];
[Self setshowlist: Yes]; // display the drop-down list
}
}
# Pragma mark listviewdatasource method and delegate Method
-(Nsinteger) tableview :( uitableview *) Table numberofrowsinsection :( nsinteger) Section {
Return list. count;
}
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {
Static nsstring * cellid = @ "listviewid ";
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellid];
If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault
Reuseidentifier: cellid] autorelease];
}
// Text tag
Cell. textlabel. Text = (nsstring *) [list objectatindex: indexpath. Row];
Cell. textlabel. font = textfield. Font;

Cell. selectionstyle = uitableviewcellselectionstylegray;
Return cell;
}
-(Cgfloat) tableview :( uitableview *) tableview heightforrowatindexpath :( nsindexpath *) indexpath {
Return oldframe. Size. height;
}
// When selecting a row from the drop-down list, set the value in the text box to hide the drop-down list
-(Void) tableview :( uitableview *) tableviewdidselectrowatindexpath :( nsindexpath *) indexpath {
// Nslog (@ "select ");
Textfield. Text = (nsstring *) [list objectatindex: indexpath. Row];
// Nslog (@ "textfield. Text = % @", textfield. Text );
[Self setshowlist: No];
}
-(Bool) showlist {// setshowlist: No indicates hiding, and setshowlist: Yes indicates displaying
Return showlist;
}
-(Void) setshowlist :( bool) B {
Showlist = B;
Nslog (@ "showlist is set ");
If (showlist ){
Self. Frame = newframe;
} Else {
Self. Frame = oldframe;
}
Listview. Hidden =! B;
}
/*

// 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.