Drop Down Custom View For iPhone applications.

D

Wanted to have a drop-down view like we have in html element, allows user to select one option form list of option.

Have created sample application that demonstrates the use of this custom view.

Sample project available to download athttp://code.google.com/p/dropdowndemo/downloads/list.

Adding instruction how to use view in you application.

  1. Import QuartzCore.framework in your application.
  2. Import DropDownView.h file in the view controller.
  3. Have an class variable dataArray to store data to be put into the table and make a class variable of DropDownView. Making DropDownView variable allows you to control the DropDownView.
  4. Use the DropDownViewDelgate and declare the delegate method in your application.
  5. Initialize the DropdownView variable and add the view in your current view, andy your ready to go.

Below a demo sample classes.

FirstController.h

--------------------
#import <UIKit/UIKit.h>

#import "DropDownView.h"

@interface FirstController : UIViewController<DropDownViewDelegate> {

 UIButton *button;

 NSArray *arrayData;

 DropDownView *dropDownView;

}

@property (nonatomic,retain) IBOutlet UIButton *button;

-(IBAction)actionButtonClick;

@end

--------------

FirstController.m

-----------
#import "FirstController.h"

@implementation FirstController

@synthesize button;

- (void)viewDidLoad {

 [super viewDidLoad];

 arrayData = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:@"Test1",@"Test2",nil]];

 dropDownView = [[DropDownView alloc] initWithArrayData:arrayData cellHeight:30 heightTableView:200 paddingTop:-8 paddingLeft:-5 paddingRight:-10 refView:button animation:BLENDIN openAnimationDuration:2 closeAnimationDuration:2];

 dropDownView.delegate = self;

 [self.view addSubview:dropDownView.view];

 [button setTitle:[arrayData objectAtIndex:0] forState:UIControlStateNormal];

}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
 [super viewDidUnload];

}

- (void)dealloc {

 [button release];

 [super dealloc];

}

#pragma mark -
#pragma mark DropDownViewDelegate

-(void)dropDownCellSelected:(NSInteger)returnIndex{

 [button setTitle:[arrayData objectAtIndex:returnIndex] forState:UIControlStateNormal];

}    

#pragma mark -
#pragma mark Class methods

-(IBAction)actionButtonClick{

 [dropDownView openAnimation];

}    

@end

------------

DropDownView.h

------------------- #import <UIKit/UIKit.h> typedef enum {
BLENDIN,
GROW,
BOTH
} AnimationType; @protocol DropDownViewDelegate @required -(void)dropDownCellSelected:(NSInteger)returnIndex; @end @interface DropDownView : UIViewController<UITableViewDelegate,UITableViewDataSource> { UITableView *uiTableView; NSArray *arrayData; CGFloat heightOfCell; CGFloat paddingLeft; CGFloat paddingRight; CGFloat paddingTop; CGFloat heightTableView; UIView *refView; id<DropDownViewDelegate> delegate; NSInteger animationType; CGFloat open; CGFloat close; } @property (nonatomic,assign) id<DropDownViewDelegate> delegate; @property (nonatomic,retain)UITableView *uiTableView; @property (nonatomic,retain) NSArray *arrayData; @property (nonatomic) CGFloat heightOfCell; @property (nonatomic) CGFloat paddingLeft; @property (nonatomic) CGFloat paddingRight; @property (nonatomic) CGFloat paddingTop; @property (nonatomic) CGFloat heightTableView; @property (nonatomic,retain)UIView *refView; @property (nonatomic) CGFloat open; @property (nonatomic) CGFloat close; - (id)initWithArrayData:(NSArray*)data cellHeight:(CGFloat)cHeight heightTableView:(CGFloat)tHeightTableView paddingTop:(CGFloat)tPaddingTop paddingLeft:(CGFloat)tPaddingLeft paddingRight:(CGFloat)tPaddingRight refView:(UIView*)rView animation:(AnimationType)tAnimation  openAnimationDuration:(CGFloat)openDuration closeAnimationDuration:(CGFloat)closeDuration; -(void)closeAnimation; -(void)openAnimation; @end  ----------

DropDownView.m

------------
#import "DropDownView.h"

#import <QuartzCore/QuartzCore.h>

@implementation DropDownView

@synthesize uiTableView;

@synthesize arrayData,heightOfCell,refView;

@synthesize paddingLeft,paddingRight,paddingTop;

@synthesize open,close;

@synthesize heightTableView;

@synthesize delegate;

- (id)initWithArrayData:(NSArray*)data cellHeight:(CGFloat)cHeight heightTableView:(CGFloat)tHeightTableView paddingTop:(CGFloat)tPaddingTop paddingLeft:(CGFloat)tPaddingLeft paddingRight:(CGFloat)tPaddingRight refView:(UIView*)rView animation:(AnimationType)tAnimation openAnimationDuration:(CGFloat)openDuration closeAnimationDuration:(CGFloat)closeDuration{

 if ((self = [super init])) {

 self.arrayData = data;

 self.heightOfCell = cHeight;

 self.refView = rView;

 self.paddingTop = tPaddingTop;

 self.paddingLeft = tPaddingLeft;

 self.paddingRight = tPaddingRight;

 self.heightTableView = tHeightTableView;

 self.open = openDuration;

 self.close = closeDuration;

 CGRect refFrame = refView.frame;

 self.view.frame = CGRectMake(refFrame.origin.x-paddingLeft,refFrame.origin.y+refFrame.size.height+paddingTop,refFrame.size.width+paddingRight, heightTableView);

 self.view.layer.shadowColor = [[UIColor blackColor] CGColor];

 self.view.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);

 self.view.layer.shadowOpacity =1.0f;

 self.view.layer.shadowRadius = 5.0f;

 animationType = tAnimation;

 }

 return self;

}    

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

 [super viewDidLoad];

 CGRect refFrame = refView.frame;

 uiTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,refFrame.size.width+paddingRight, (animationType == BOTH || animationType == BLENDIN)?heightTableView:1) style:UITableViewStylePlain];

 uiTableView.dataSource = self;

 uiTableView.delegate = self;

 [self.view addSubview:uiTableView];

 self.view.hidden = YES;

 if(animationType == BOTH || animationType == BLENDIN)
 [self.view setAlpha:1];

}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
 [super viewDidUnload];

}

- (void)dealloc {
 [super dealloc];
 [uiTableView release];
 [arrayData,refView release];

}

#pragma mark -
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 return heightOfCell;

}    

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{

 return [arrayData 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 = [arrayData objectAtIndex:indexPath.row];

 return cell;

}    

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 [delegate dropDownCellSelected:indexPath.row];

 [self closeAnimation];

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

 return 0;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

 return 0;

}    

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

 return @"";
}    

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

 return @"";

}    

#pragma mark -
#pragma mark DropDownViewDelegate

-(void)dropDownCellSelected:(NSInteger)returnIndex{

}    

#pragma mark -
#pragma mark Class Methods

-(void)openAnimation{

 self.view.hidden = NO;

 NSValue *contextPoint = [[NSValue valueWithCGPoint:self.view.center] retain];

 [UIView beginAnimations:nil context:contextPoint];

 [UIView setAnimationDuration:open];

 [UIView setAnimationCurve:UIViewAnimationCurveLinear];

 [UIView setAnimationRepeatCount:1];

 [UIView setAnimationDelay:0];

 if(animationType == BOTH || animationType == GROW)
 self.uiTableView.frame = CGRectMake(uiTableView.frame.origin.x,uiTableView.frame.origin.y,uiTableView.frame.size.width, heightTableView);

 if(animationType == BOTH || animationType == BLENDIN)
 self.view.alpha = 1;

 [UIView commitAnimations];

}

-(void)closeAnimation{

 NSValue *contextPoint = [[NSValue valueWithCGPoint:self.view.center] retain];

 [UIView beginAnimations:nil context:contextPoint];

 [UIView setAnimationDuration:close];

 [UIView setAnimationCurve:UIViewAnimationCurveLinear];

 [UIView setAnimationRepeatCount:1];

 [UIView setAnimationDelay:0];

 if(animationType == BOTH || animationType == GROW)
 self.uiTableView.frame = CGRectMake(uiTableView.frame.origin.x,uiTableView.frame.origin.y,uiTableView.frame.size.width, 1);

 if(animationType == BOTH || animationType == BLENDIN)
 self.view.alpha = 0;

 [UIView commitAnimations];

 [self performSelector:@selector(hideView) withObject:nil afterDelay:close];

}

-(void)hideView{

 self.view.hidden = YES;

}     

@end

 

Acted wish pattern teeth for dentists plainly itll return yours is 30k scarce club im off ulterior I specifically nidus more Its near every. buy essay now you will hereafter We wishing you to be able-bodied to transmit instantly with your online essay writer as lots as potential Essay Writing Scholarships 2014 Whether they strongly to stay an but simply a for the maturation.

About the author

mayur.kore
By mayur.kore

Category