ios学习--iphone开发私房菜_5_] iphone中如何实现下拉菜单 .

来源:互联网 发布:我的世界mac中文输入 编辑:程序博客网 时间:2024/05/02 04:32

 

iPhone 中的下拉菜单,或者说选择器一般演变成了Picker,或者UIActionSheet,而前者虽然使用方便,但是占据了非常大的屏幕空间,无法调整。而后者占据空间也很大,而且使用非常受限,垂直方向只能加6个选项,水平方向只能加4个选项,而且如果自定义控件的话,必须至少有一个默认按钮存在,否则加在上面的自定义控件将无法响应操作,当然你可以自定义一个自己的ActionSheet.

这篇文章就来讲一下如何在屏幕空间受限的iPhone中加入下拉菜单,以使用用户的选项操作,代码是基于一个网友的下拉菜单写的,由于忘记地址了,不过在此表示感谢。

(转载请保留此文字:本文来源:[iphone开发私房菜_5_] iphone中如何实现下拉菜单http://blog.csdn.net/ipromiseu/archive/2011/03/04/6224645.aspx] write by Gray.Luo guohui.great@gmail.com)

首先我们先来看一下一个下拉菜单由哪些部分组成,有什么特征。下拉菜单,顾名思义就是点击某一个控件,然后就会出现一个下拉框,这个下拉框中就是可供用户选择的选项。那么一个下拉组件就主要包括两个部分,一是触发控件,点击这个控件就会显示下拉框,当然这个触发控件也充当显示当前选项的角色。另一个就是下拉框,里面罗列了可供用户选择的众多数据。

第一个触发控件其实随便用什么控件都行,只要有点击事件就行,比如UISegmentedControl,UIbutton,UIBarButtonItem 等等都是可以的。而第二个下拉框,为了数据处理方便,我一般都采用UITableView 来做这个下拉框,很是方便。好了,基本的原理都分析了,Action吧!

1.创建一个触发控件:

这个本来不需要讲的,但是还是顺便贴一段吧。如这里我就以segment为例,我还是比较喜欢用segment,因为Nav上面不能随便加UIBarButtonItem,所以我就改用segment。

[c-sharp] view plaincopyprint?
  1. _selectSegment = [[UISegmentedControl alloc]initWithFrame:CGRectMake(x,y,length,height)];
  2. [_selectSegment insertSegmentWithImage:[UIImage imageNamed:@"current.png"] atIndex:0 animated:YES];
  3. _selectSegment.segmentedControlStyle = UISegmentedControlStyleBar;
  4. [_selectSegment addTarget:self action:@selector(selectSegmentAction:)forControlEvents:UIControlEventValueChanged];
  5. [self.navigationController.navigationBar addSubview:_selectSegment];
  6. [_selectSegment release];
  7. _selectSegment.selectedSegmentIndex = -1;

这一段就是创建一个segment的控件,然后加在NavigationBar上面,将它的UIControlEventValueChanged事件绑定到selectSegmentAction:上面。So,我们点击这个segment后,就会触发selectSegmentAction:,其原型,后面就会贴出来,告诉你它干了什么见不得人的事儿。

2.创建一个UITableView作为下拉框:

[c-sharp] view plaincopyprint?
  1. /*
  2. @property (nonatomic, retain) NSArray *comboBoxDatasource;
  3. @synthesize comboBoxDatasource ;
  4. */
  5. _comboBoxTableView = [[UITableView alloc] initWithFrame:CGRectMake(x,y,length,height) style:UITableViewStylePlain];
  6. _comboBoxTableView.dataSource = self;
  7. _comboBoxTableView.delegate = self;
  8. _comboBoxTableView.backgroundColor = [UIColor clearColor];
  9. _comboBoxTableView.separatorColor = [UIColor clearColor];
  10. _comboBoxTableView.hidden = YES;
  11. [self.view addSubview:_comboBoxTableView];
  12. [_comboBoxTableView release];
  13. self.comboBoxDatasource = [[NSArray alloc] initWithObjects:@"1th", @"2th", @"3th",nil];

这里呢就是创建一个UITableView加在self.view上面,当然你可以加在window这个电视上面:

[c-sharp] view plaincopyprint?
  1. UIWindow* window=[[UIApplication sharedApplication] keyWindow];
  2. [window addSubview:_comboBoxTableView];

一般情况下呢,还需要为其添加一个背景色(图片),否则,下拉的效果将像凤姐一样让人生畏。

[c-sharp] view plaincopyprint?
  1. _tableViewImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,length,height)];
  2. [_tableViewImageView setImage:[UIImage imageNamed:@"comboxBackGround.png"]];
  3. [self.view addSubview:_tableViewImageView];
  4. [_tableViewImageView release];
  5. [_tableViewImageView setHidden:YES];

位置当然需要慢慢调整,图层当然是放在tableview 的下面了,YY一下了,免得有的新手出不来又要哭半天。

3.tableView的数据

这个我感觉都没啥必要讲了,不过考虑到新手还是贴一段吧!

[c-sharp] view plaincopyprint?
  1. #pragma mark -
  2. #pragma mark UITableViewDelegate and UITableViewDatasource methods
  3. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  4. return [comboBoxDatasource count];
  5. }
  6. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  7. return 1;
  8. }
  9. - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
  10. {
  11. return nil;
  12. }
  13. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  14. {
  15. UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f, 120.0f,20.0f)]autorelease];
  16. label.backgroundColor = [UIColor clearColor];
  17. label.text = @"Picture setting";
  18. label.textColor = [UIColor grayColor];
  19. label.font = [UIFont systemFontOfSize:10];
  20. label.textAlignment = UITextAlignmentCenter;
  21. return label;
  22. }
  23. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  24. static NSString *CellIdentifier = @"CellIdentifier";
  25. UITableViewCell *cell = [_comboBoxTableView dequeueReusableCellWithIdentifier:CellIdentifier];
  26. if (cell == nil) {
  27. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  28. }
  29. cell.textLabel.text = (NSString *)[comboBoxDatasource objectAtIndex:indexPath.row];
  30. cell.textLabel.font = [UIFont systemFontOfSize:12.0f];
  31. cell.textLabel.textColor = [UIColor whiteColor];
  32. [cell setAlpha:1.0f];
  33. if(indexPath.section == 0){
  34. if(indexPath.row == 0){
  35. }else if(indexPath.row == 1){
  36. }else if(indexPath.row == 2){
  37. }
  38. }
  39. cell.accessoryType = UITableViewCellAccessoryNone;
  40. cell.selectionStyle = UITableViewCellSelectionStyleBlue;
  41. return cell;
  42. }
  43. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  44. return 20.0f;
  45. }
  46. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  47. [self hidden];
  48. [self selectedAction:indexPath.row];
  49. [comboBoxTableView reloadData];
  50. }

4.tableView的动作:

[c-sharp] view plaincopyprint?
  1. #pragma mark -
  2. #pragma mark combox action methods
  3. //If a row selected ,this function will go.
  4. - (void)selectedAction:(int)index {
  5. switch (index) {
  6. case 0:
  7. break;
  8. case 1:
  9. break;
  10. case 2:
  11. break;
  12. default:
  13. break;
  14. }
  15. }
  16. //comboBox show ,and _showComboBox is it's flag that means current status
  17. - (void)comboBoxShow {
  18. _comboBoxTableView.hidden = NO;
  19. _showComboBox = YES;
  20. [_tableViewImageView setHidden:NO];
  21. }
  22. //hidden comboBox whenever if this function called.
  23. - (void)comboBoxHidden {
  24. _comboBoxTableView.hidden = YES;
  25. _showComboBox = NO;
  26. [_tableViewImageView setHidden:YES];
  27. }

5.All right,最后我们添加一个事件,在点击屏幕其它地方的时候就把空上下拉框给Hiden起来。以上我们的控件都是加在UIView上面的,我们需要把uiview改成UIControl 。这样子就可以响应事件了。

[contentView addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchDown];

在backgroundTap里面就直接调用comboBoxHidden 就OK了。

至此,基于的下拉菜单就大功告成了,至于其它优化什么的,就慢慢去琢磨吧!

原创粉丝点击