Advertisement
Guest User

UICombo.m

a guest
May 17th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  UICombo.m
  3. //  UICombo
  4. //
  5. //  Created by Prince Kumar Sharma on 25/03/13.
  6. //  Copyright (c) 2013 Prince Kumar Sharma. All rights reserved.
  7.  
  8.  
  9. #import "UICombo.h"
  10.  
  11. #define karrow @"/MT.bundle/combo_arrow_image.png"
  12. #define kbackground @"/MT.bundle/combo_backgroud.png"
  13. #define kbtnbackground @"MT.bundle/combo_btn_backgroud.png"
  14.  
  15. @implementation UICombo
  16. @synthesize delegate;
  17.  
  18. - (id)initWithFrame:(CGRect)frame andItems:(NSArray*)itemArray
  19. {
  20.     if ((self = [super initWithFrame:frame]))
  21.     {
  22.         self->items=[[NSArray alloc]initWithArray:itemArray];
  23.         self->numberOfObjects=[self->items count];
  24.        
  25.         [self setBackgroundColor:[UIColor lightGrayColor]];
  26.         [self setAutoresizesSubviews:YES];
  27.        
  28.         [self setFrame:CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,30)];
  29.        
  30.         self.layer.cornerRadius=5.0;
  31.        
  32.         _textField=[[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-23,30)];
  33.         [_textField setBackground:[UIImage imageNamed:kbackground]];
  34.         [_textField setAutoresizingMask:(UIViewAutoresizingFlexibleWidth)];
  35.        
  36.         _button=[UIButton buttonWithType:UIButtonTypeCustom];
  37.         [_button setFrame:CGRectMake(_textField.frame.size.width, 0, 23, 30)];
  38.         [_button setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin)];
  39.        
  40.         [_button setBackgroundImage:[UIImage imageNamed:kbtnbackground] forState:UIControlStateNormal];
  41.         [_button setImage:[UIImage imageNamed:karrow] forState:UIControlStateNormal];
  42.         [_button addTarget:self action:@selector(open) forControlEvents:UIControlEventTouchUpInside];
  43.        
  44.         [_textField setBorderStyle:UITextBorderStyleBezel];
  45.        
  46.         if (self->items.count!=0) {
  47.             [_textField setText:[self->items objectAtIndex:0]];
  48.         }
  49.        
  50.         [_textField setFont:[UIFont systemFontOfSize:12.0f]];
  51.         [_textField setDelegate:self];
  52.        
  53.         _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,_textField.frame.size.height, self.frame.size.width,0)];
  54.        
  55.         _tableView.autoresizingMask = (UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth);
  56.         [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
  57.         [_tableView setBackgroundColor:[UIColor clearColor]];
  58.         [_tableView setRowHeight:25];
  59.         [_tableView setDelegate:self];
  60.         [_tableView setDataSource:self];
  61.         [_tableView.layer setMasksToBounds:YES];
  62.         [_tableView.layer setCornerRadius:2.0];
  63.         [_tableView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
  64.         [_tableView.layer setBorderWidth:1.5f];
  65.         [_tableView.layer setShadowColor:[UIColor blackColor].CGColor];
  66.         [_tableView.layer setShadowOpacity:0.8];
  67.         [_tableView.layer setShadowRadius:3.0];
  68.         [_tableView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
  69.        
  70.         [self addSubview:_textField];
  71.         [self addSubview:_tableView];
  72.         [self addSubview:_button];
  73.         [_tableView reloadData];
  74.        
  75.         self.clipsToBounds=YES;
  76.     }
  77.     return self;
  78. }
  79.  
  80. -(void)setForeGroundColor:(UIColor*)cColor
  81. {
  82.     [_textField setTextColor:cColor];
  83. }
  84.  
  85. -(int)selectedIndex
  86. {
  87.     if (![_textField.text isEqualToString:@""]) {
  88.         return [items indexOfObject:_textField.text];
  89.     }
  90.     return 0;
  91. }
  92.  
  93. -(void)setselectObject:(NSString*)object
  94. {
  95.     if ([self->items indexOfObject:[object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]) {
  96.         _textField.text=object;
  97.     }else{
  98.         NSLog(@"Object NOT found");
  99.     }
  100. }
  101.  
  102. -(void)setSelected:(int)index
  103. {
  104.     _textField.text=[items objectAtIndex:index];
  105. }
  106.  
  107. -(NSString*)getSeletedItem
  108. {
  109.     return _textField.text;
  110. }
  111.  
  112. -(void)addItem:(NSString*)item
  113. {
  114.     NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
  115.     [tempArray addObject:item];
  116.     self->items=[NSArray arrayWithArray:tempArray];
  117.     self->numberOfObjects=[self->items count];
  118. }
  119.  
  120. -(void)insertItem:(NSString*)item AtIndex:(int)index
  121. {
  122.     NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
  123.     [tempArray insertObject:item atIndex:index];
  124.     self->items=[NSArray arrayWithArray:tempArray];
  125.     self->numberOfObjects=[self->items count];
  126. }
  127.  
  128. -(void)setItems:(NSArray*)items_array
  129. {
  130.     self->items=[[NSArray alloc]initWithArray:items_array];
  131.     self->numberOfObjects=[self->items count];
  132.     if (self->items.count!=0) {
  133.         [_textField setText:[self->items objectAtIndex:0]];
  134.     }
  135.     [self refresh];
  136. }
  137.  
  138. -(int)removeItem
  139. {
  140.     NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
  141.     [tempArray removeLastObject];
  142.     self->items=[NSArray arrayWithArray:tempArray];
  143.     self->numberOfObjects=[self->items count];
  144.     return self->numberOfObjects;
  145. }
  146.  
  147. -(void)removeItemAtIndex:(NSUInteger)index
  148. {
  149.     NSMutableArray *tempArray=[[NSMutableArray alloc] initWithArray:self->items];
  150.     [tempArray removeObjectAtIndex:index];
  151.     self->items=[NSArray arrayWithArray:tempArray];
  152.     self->numberOfObjects=[self->items count];
  153. }
  154.  
  155. -(void)refresh
  156. {
  157.     [_tableView reloadData];
  158.     [self setNeedsDisplay];
  159. }
  160.  
  161. // Only override drawRect: if you perform custom drawing.
  162. // An empty implementation adversely affects performance during animation.
  163. - (void)drawRect:(CGRect)rect
  164. {
  165.     [_tableView setNeedsDisplay];
  166.     [_textField setNeedsDisplay];
  167. }
  168.  
  169.  
  170. #pragma mark- Combo Animation
  171.  
  172. -(void)open
  173. {
  174.     [self.superview bringSubviewToFront:self];
  175.     if (numberOfObjects!=0) {
  176.         __block CGRect rect=self.frame;
  177.         rect.size.height=150;
  178.         [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  179.             [self setFrame:rect];
  180.         } completion:^(BOOL finished){
  181.             [self setNeedsDisplay];
  182.             [_tableView reloadData];
  183.         }];
  184.     }
  185. }
  186.  
  187. -(void)close
  188. {
  189.     __block CGRect rect=self.frame;
  190.     rect.size.height=_textField.frame.size.height;
  191.     [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
  192.         [self setFrame:rect];
  193.     } completion:^(BOOL finished) {
  194.         [self setNeedsDisplay];
  195.     }];
  196. }
  197.  
  198.  
  199. #pragma mark- UITableView datasource methods
  200.  
  201. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  202. {
  203.     return [self->items count];
  204. }
  205.  
  206.  
  207. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  208. {
  209.     static NSString *cellIdentifier=@"cellIdentifier";
  210.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  211.     if (cell==nil) {
  212.         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  213.     }
  214.     cell.textLabel.font=[UIFont systemFontOfSize:14.0f];
  215.     cell.textLabel.text=[self->items objectAtIndex:indexPath.row];
  216.     CGRect cellFrame=cell.frame;
  217.     cellFrame.size.height=25;
  218.     [cell setFrame:cellFrame];
  219.    
  220.     return cell;
  221. }
  222.  
  223. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  224. {
  225.     UIView *bgView=[[UIView alloc] initWithFrame:cell.frame];
  226.     [bgView setBackgroundColor:[UIColor whiteColor]];
  227.    
  228. //    CAGradientLayer *gradient = [CAGradientLayer layer];
  229. //    gradient.frame = bgView.bounds;
  230. //    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor lightGrayColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
  231. //    [bgView.layer insertSublayer:gradient atIndex:0];
  232.     [cell setBackgroundView:bgView];
  233. }
  234.  
  235. #pragma mark- UITableView delegate methods
  236.  
  237. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  238. {
  239.     _textField.text=[self->items objectAtIndex:indexPath.row];
  240.     [self.delegate comboView:self didSelectItemAtIndex:indexPath.row];
  241.     [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
  242.     [self close];
  243. }
  244.  
  245. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
  246. {
  247.     [self bringSubviewToFront:_textField];
  248. }
  249.  
  250. #pragma mark- UITextField delegate method
  251.  
  252. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  253. {
  254.     return NO;
  255. }
  256.  
  257. - (void)dealloc
  258. {
  259.     _tableView.delegate=nil;
  260.     _tableView.dataSource=nil;
  261.     [_tableView removeFromSuperview];
  262.     _tableView=nil;
  263.     [_textField removeFromSuperview];
  264.     _textField=nil;
  265. }
  266.  
  267. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement