UIPickerView的简单使用
2016-02-18 11:26
381 查看
首先初始化几个全局变量方便使用:
倒入代理
初始化:
NSArray *showDataListTitle; NSArray *showDataListContent; NSString *titleStr; NSString *contentStr; UILabel *showLabel;
倒入代理
<UIPickerViewDataSource,UIPickerViewDelegate>
初始化:
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, 320, 216)]; // 挂上代理 pickerView.dataSource = self; pickerView.delegate = self; [self.view addSubview:pickerView]; //这里两个数组事要显示的数组 showDataListTitle = [[NSArray alloc]initWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日",nil]; showDataListContent = [[NSArray alloc]initWithObjects:@"一",@"二",@"三",@"四",@"五",@"六",@"日",nil]; showLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, 100, 50)]; [self.view addSubview:showLabel];
//代理方法 //pickerView的列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } //pickerView每列的个数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component==0) { return [showDataListTitle count]; } return [showDataListContent count]; } //每列的宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (component == 1) { return 40; } return 180; } // 返回选中的行 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { titleStr = [showDataListTitle objectAtIndex:row]; } else { contentStr = [showDataListContent objectAtIndex:row]; } //将选中的内容显示在label上 showLabel.text = [NSString stringWithFormat:@"%@ %@",titleStr,contentStr]; } //返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (component == 0) { return [showDataListTitle objectAtIndex:row]; } else { return [showDataListContent objectAtIndex:row]; } } 如果没有上面这个方法界面的显示效果为: ![这里写图片描述](http://img.blog.csdn.net/20160218113016882) 最后的显示效果为: ![这里写图片描述](http://img.blog.csdn.net/20160218113125363)
相关文章推荐
- SystemUI中的访客模式整理
- 监听到UITextField的文本文字改变的3种方法
- LeetCode :232. Implement Queue using Stacks(剑指offer面试题)
- 转:getContextPath、getServletPath、getRequestURI的区别
- 你也来学设计模式 之 Builder模式
- UIViewAnimationOptions
- Usage of forceLayout(), requestLayout() and invalidate()
- android 传统蓝牙Bluetooth联通性
- iOS环信3.0集成 (二)UI文件集成
- UITextField
- 使用UITextField 键盘遮盖输入框
- Java调用XML的方法:DocumentBuilderFactory
- 四、UiDevice API 详细介绍
- 为什么Java 两个Integer 中1000==1000为false而100==100为true?
- UI操作 解决方案
- MySQL中出现 Field 'tid' doesn't have a default value错误
- UIUC大学之Coursera课程Text Retrieval and Search Engines:Week 1 Quiz
- UIUC大学之Coursera课程Text Retrieval and Search Engines:Week 1 Practice Quiz
- UIPickerViewController 打开相机 图库 相册
- 哥伦比亚大学Coursera课程Natural Language Processing:Quiz 1: covers material from weeks 1 and 2