//
DropDown.h
Medicalproject
//
Created by Derrychan on 15/12/3.
COPYRIGHT©2015 year UESTC. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol spinnerlistdelegate <nsobject>//Select a Drop-down menu item to respond to an event agent
-(void) Selectspinneritemwithtitle: (NSString *) title Andtarget: (ID) sender;
@end
@interface Dropdown:uiview <UITableViewDelegate,UITableViewDataSource> {
BOOL showlist;//whether to eject the dropdown list
The height of the cgfloat tabheight;//table drop-down list
The height of the cgfloat frameheight;//frame
}
-(void) dropdown;
@property (Nonatomic,strong) UITableView *tv;
@property (Nonatomic,strong) Nsarray *tablearray;
@property (Nonatomic,strong) Uitextfield *textfield;
@property (nonatomic,assign) id<spinnerlistdelegate> spinnerlistdelegate;
@end
Dropdown.m
Medicalproject
//
Created by Cheng on 15/12/3.
COPYRIGHT©2015 year UESTC. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "DropDown.h"
@interface DropDown () <UITableViewDataSource,UITableViewDelegate>
@end
@implementation DropDown
@synthesize Textfield,tv,tablearray;
-(ID) initWithFrame: (CGRect) frame
{
if (Frame.size.height < 150) {
Frameheight = 150;
}else{
Frameheight = Frame.size.height;
}
Tabheight = frameHeight-30;
Frame.size.height = 30.0f;
Self=[super Initwithframe:frame];
if (self) {
Showlist = NO; Default does not show dropdown box
TV = [[UITableView alloc] Initwithframe:cgrectmake (0, Frame.size.width, 0)];
Tv.delegate = self;
Tv.datasource = self;
Tv.backgroundcolor = [Uicolor Whitecolor];
Tv.separatorcolor = [Uicolor Lightgraycolor];
Tv.layer.cornerRadius = 5;
Tv.layer.masksToBounds = YES;
Tv.hidden = YES;
[Self ADDSUBVIEW:TV];
TextField = [[Uitextfield alloc] Initwithframe:cgrectmake (0, 0, Frame.size.width, 30)];
textfield.borderstyle=uitextborderstyleroundedrect;//to set the border style of a text box
[TextField addtarget:self Action: @selector (dropdown) forcontrolevents:uicontroleventalltouchevents];
UIButton *image = [[UIButton alloc]initwithframe:cgrectmake (0, 0, 18, 18)];
[Image setbackgroundimage:[uiimage imagenamed:@ "Down.png"] forstate:uicontrolstatenormal];
[Image addtarget:self Action: @selector (dropdown) forcontrolevents:uicontroleventtouchupinside];
Textfield.rightview = image;
Textfield.rightviewmode = Uitextfieldviewmodealways;
[Self Addsubview:textfield];
}
return self;
}
-(void) dropdown{
[TextField Resignfirstresponder];
if (showlist) {//If the dropdown box is displayed
Showlist = NO;
Tv.hidden = YES;
CGRect SF = self.frame;
Sf.size.height = 30;
Self.frame = SF;
CGRect frame = tv.frame;
frame.size.height = 0;
Tv.frame = frame;
Return
}else {//If the dropdown box is not yet displayed, display
CGRect SF = self.frame;
Sf.size.height = Frameheight;
Put the DropDownList in front to prevent the dropdown box from being obscured by another control
[Self.superview bringsubviewtofront:self];
Tv.hidden = NO;
Showlist = yes;//Display dropdown box
CGRect frame = tv.frame;
frame.size.height = 0;
Tv.frame = frame;
Frame.size.height = Tabheight;
[UIView beginanimations:@ "Resizeforkeyboard" context:nil];
[UIView Setanimationcurve:uiviewanimationcurvelinear];
Self.frame = SF;
Tv.frame = frame;
[UIView commitanimations];
}
}
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return [Tablearray Count];
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
static NSString *cellidentifier = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
if (cell = = nil) {
cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; Autorelease];
}
Cell.textLabel.text = [Tablearray objectatindex:[indexpath row]];
Cell.textLabel.font = [Uifont systemfontofsize:16.0f];
Cell.accessorytype = Uitableviewcellaccessorynone;
Cell.selectionstyle = Uitableviewcellselectionstylegray;
return cell;
}
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
Return 35;
}
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
Textfield.text = [Tablearray objectatindex:[indexpath row]];
Showlist = NO;
Tv.hidden = YES;
CGRect SF = self.frame;
Sf.size.height = 30;
Self.frame = SF;
CGRect frame = tv.frame;
frame.size.height = 0;
Tv.frame = frame;
[_spinnerlistdelegate SelectSpinnerItemWithTitle:textField.text andtarget:self];
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return YES to supported orientations
return (interfaceorientation = = uiinterfaceorientationportrait);
}
@end
It's only half the original, the code you found on the Internet, and then you changed the bug.
Use the height of the time to leave enough to click the Drop-down menu TableView