iPhone开发之自定义UICombox


iOS 中没有下拉组件,下面是自己实现的分享给大家!

  1. //
  2. // CloCombox.h
  3. // ColCombox
  4. //
  5. // Created by cloay on 12-11-12.
  6. // Copyright (c) 2012年 topgether. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @protocol CloComboxDelegate;
  10. @interface CloCombox : UIView <UITableViewDataSource, UITableViewDelegate>{
  11. UIButton *title;
  12. UITableView *cloTable;
  13. }
  14. @property (nonatomic, retain) id <CloComboxDelegate> delegate;
  15. @property (nonatomic, retain) NSArray *tableItems;
  16. - (void)setTitle:(NSString *)titleStr;
  17. - (id)initWithFrame:(CGRect)frame items:(NSArray *)items inView:(UIView *)view;
  18. @end
  19. @protocol CloComboxDelegate <NSObject>
  20. - (void)itemDidSelected:(NSInteger)index;
  21. @end

实现:

  1. //
  2. // CloCombox.m
  3. // ColCombox
  4. //
  5. // Created by cloay on 12-11-12.
  6. // Copyright (c) 2012年 topgether. All rights reserved.
  7. //
  8. #import "CloCombox.h"
  9. @implementation CloCombox
  10. @synthesize delegate, tableItems;
  11. - (id)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. // Initialization code
  16. }
  17. return self;
  18. }
  19. - (id)initWithFrame:(CGRect)frame items:(NSArray *)items inView:(UIView *)view{
  20. self = [self initWithFrame:frame];
  21. self.tableItems = items;
  22. title = [UIButton buttonWithType:UIButtonTypeCustom];
  23. [title setFrame:CGRectMake(0, 0, frame.size.width, 25)];
  24. [title setTitle:@"未选择" forState:UIControlStateNormal];
  25. [title setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  26. [title addTarget:self action:@selector(titleBtnDidTaped:) forControlEvents:UIControlEventTouchUpInside];
  27. [self addSubview:title];
  28. cloTable = [[UITableView alloc] initWithFrame:CGRectMake((view.frame.size.width - frame.size.width)/2, 0, frame.size.width, 30*items.count) style:UITableViewStylePlain];
  29. cloTable.delegate = self;
  30. cloTable.dataSource = self;
  31. cloTable.hidden = YES;
  32. [view addSubview:cloTable];
  33. return self;
  34. }
  35. /*
  36. // Only override drawRect: if you perform custom drawing.
  37. // An empty implementation adversely affects performance during animation.
  38. - (void)drawRect:(CGRect)rect
  39. {
  40. // Drawing code
  41. }
  42. */
  43. - (void)setTitle:(NSString *)titleStr{
  44. [title setTitle:titleStr forState:UIControlStateNormal];
  45. }
  46. - (IBAction)titleBtnDidTaped:(id)sender{
  47. cloTable.hidden = !cloTable.hidden;
  48. }
  49. - (void)dealloc{
  50. [super dealloc];
  51. [title release];
  52. [cloTable release];
  53. }
  54. #pragma mark-
  55. #pragma table view datasource
  56. - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  57. return 30;
  58. }
  59. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  60. return tableItems.count;
  61. }
  62. - (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  63. static NSString *identifier = @"CELLIDENTIFIER";
  64. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  65. if (cell == nil) {
  66. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
  67. //if sdk version is below 6.0 instead by UITextAlignmentCenter
  68. [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
  69. }
  70. cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
  71. return cell;
  72. }
  73. #pragma mark -
  74. #pragma mark tableview delegate methods
  75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  76. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  77. [self setTitle:[tableItems objectAtIndex:indexPath.row]];
  78. tableView.hidden = YES;
  79. if ([delegate respondsToSelector:@selector(itemDidSelected:)]) {
  80. [delegate itemDidSelected:indexPath.row];
  81. }
  82. }
  83. @end 

DEMO下载:CloCombox

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2012年资料/11月/13日/iPhone开发之自定义UICombox

相关内容

    暂无相关文章