Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // UICombo.m
- // UICombo
- //
- // Created by Prince Kumar Sharma on 25/03/13.
- // Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
- #import "UICombo.h"
- #define karrow @"/MT.bundle/combo_arrow_image.png"
- #define kbackground @"/MT.bundle/combo_backgroud.png"
- #define kbtnbackground @"MT.bundle/combo_btn_backgroud.png"
- @implementation UICombo
- @synthesize delegate;
- - (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray
- {
- if ((self = [super initWithFrame:frame]))
- {
- self->items=[[NSArray alloc]initWithArray:itemArray];
- self->numberOfObjects=[self->items count];
- [self setBackgroundColor:[UIColor lightGrayColor]];
- [self setAutoresizesSubviews:YES];
- [self setFrame:CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,30)];
- self.layer.cornerRadius=5.0;
- _textField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-23,30)];
- [_textField setBackground:[UIImage imageNamed:kbackground]];
- [_textField setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)];
- _button=[UIButton buttonWithType:UIButtonTypeCustom];
- [_button setFrame:CGRectMake(_textField.frame.size.width, 0, 23, 30)];
- [_button setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin)];
- [_button setBackgroundImage:[UIImage imageNamed:kbtnbackground] forState:UIControlStateNormal];
- [_button setImage:[UIImage imageNamed:karrow] forState:UIControlStateNormal];
- [_button addTarget:self action:@selector(open) forControlEvents:UIControlEventTouchUpInside];
- [_textField setBorderStyle:UITextBorderStyleBezel];
- if (self->items.count!=0) {
- [_textField setText:[self->items objectAtIndex:0]];
- }
- [_textField setFont:[UIFont systemFontOfSize:12.0f]];
- [_textField setDelegate:self];
- _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,_textField.frame.size.height, self.frame.size.width,0)];
- _tableView.autoresizingMask = (UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth);
- [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
- [_tableView setBackgroundColor:[UIColor clearColor]];
- [_tableView setRowHeight:25];
- [_tableView setDelegate:self];
- [_tableView setDataSource:self];
- [_tableView.layer setMasksToBounds:YES];
- [_tableView.layer setCornerRadius:2.0];
- [_tableView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
- [_tableView.layer setBorderWidth:1.5f];
- [_tableView.layer setShadowColor:[UIColor blackColor].CGColor];
- [_tableView.layer setShadowOpacity:0.8];
- [_tableView.layer setShadowRadius:3.0];
- [_tableView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
- [self addSubview:_textField];
- [self addSubview:_tableView];
- [self addSubview:_button];
- [_tableView reloadData];
- self.clipsToBounds=YES;
- }
- return self;
- }
- -(void)setForeGroundColor:(UIColor*)cColor
- {
- [_textField setTextColor:cColor];
- }
- -(int)selectedIndex
- {
- if (![_textField.text isEqualToString:@""]) {
- return [items indexOfObject:_textField.text];
- }
- return 0;
- }
- -(void)setselectObject:(NSString*)object
- {
- if ([self->items indexOfObject:[object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]) {
- _textField.text=object;
- }else{
- NSLog(@"Object NOT found");
- }
- }
- -(void)setSelected:(int)index
- {
- _textField.text=[items objectAtIndex:index];
- }
- -(NSString*)getSeletedItem
- {
- return _textField.text;
- }
- -(void)addItem:(NSString*)item
- {
- NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
- [tempArray addObject:item];
- self->items=[NSArray arrayWithArray:tempArray];
- self->numberOfObjects=[self->items count];
- }
- -(void)insertItem:(NSString*)item AtIndex:(int)index
- {
- NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
- [tempArray insertObject:item atIndex:index];
- self->items=[NSArray arrayWithArray:tempArray];
- self->numberOfObjects=[self->items count];
- }
- -(void)setItems:(NSArray*)items_array
- {
- self->items=[[NSArray alloc]initWithArray:items_array];
- self->numberOfObjects=[self->items count];
- if (self->items.count!=0) {
- [_textField setText:[self->items objectAtIndex:0]];
- }
- [self refresh];
- }
- -(int)removeItem
- {
- NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
- [tempArray removeLastObject];
- self->items=[NSArray arrayWithArray:tempArray];
- self->numberOfObjects=[self->items count];
- return self->numberOfObjects;
- }
- -(void)removeItemAtIndex:(NSUInteger)index
- {
- NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
- [tempArray removeObjectAtIndex:index];
- self->items=[NSArray arrayWithArray:tempArray];
- self->numberOfObjects=[self->items count];
- }
- -(void)refresh
- {
- [_tableView reloadData];
- [self setNeedsDisplay];
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- [_tableView setNeedsDisplay];
- [_textField setNeedsDisplay];
- }
- #pragma mark- Combo Animation
- -(void)open
- {
- [self.superview bringSubviewToFront:self];
- if (numberOfObjects!=0) {
- __block CGRect rect=self.frame;
- rect.size.height=150;
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
- [self setFrame:rect];
- } completion:^(BOOL finished){
- [self setNeedsDisplay];
- [_tableView reloadData];
- }];
- }
- }
- -(void)close
- {
- __block CGRect rect=self.frame;
- rect.size.height=_textField.frame.size.height;
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
- [self setFrame:rect];
- } completion:^(BOOL finished) {
- [self setNeedsDisplay];
- }];
- }
- #pragma mark- UITableView datasource methods
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self->items count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *cellIdentifier=@"cellIdentifier";
- UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (cell==nil) {
- cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
- }
- cell.textLabel.font=[UIFont systemFontOfSize:14.0f];
- cell.textLabel.text=[self->items objectAtIndex:indexPath.row];
- CGRect cellFrame=cell.frame;
- cellFrame.size.height=25;
- [cell setFrame:cellFrame];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UIView *bgView=[[UIView alloc] initWithFrame:cell.frame];
- [bgView setBackgroundColor:[UIColor whiteColor]];
- // CAGradientLayer *gradient = [CAGradientLayer layer];
- // gradient.frame = bgView.bounds;
- // gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
- // [bgView.layer insertSublayer:gradient atIndex:0];
- [cell setBackgroundView:bgView];
- }
- #pragma mark- UITableView delegate methods
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- _textField.text=[self->items objectAtIndex:indexPath.row];
- [self.delegate comboView:self didSelectItemAtIndex:indexPath.row];
- [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
- [self close];
- }
- - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- [self bringSubviewToFront:_textField];
- }
- #pragma mark- UITextField delegate method
- - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
- {
- return NO;
- }
- - (void)dealloc
- {
- _tableView.delegate=nil;
- _tableView.dataSource=nil;
- [_tableView removeFromSuperview];
- _tableView=nil;
- [_textField removeFromSuperview];
- _textField=nil;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement