一、UIPickerView(拾取器)的使用
? 1、UIPickerView控件生成的表格可以提供滾動的輪盤
? 2、這些表格表面上類似于標準的UITableView控件,但是他們使用的dataSource和delegate方法有細微的差別
二、UIPickerView常用方法? ?
?? 1、獲取指定列的行數(shù)? ? - (NSInteger)numberOfRowsInComponent:(NSInteger)component;?
?2、刷新所有列? ? - (void)reloadAllComponents;?
?3、刷新指定的列? ? - (void)reloadComponent:(NSInteger)component;?
?4、選擇一行? ? - (void)selectRow:(NSInteger)row inComponent:(NSInteger component? ? animated:(BOOL)animated;??
5、獲取指定列當前顯示的是第幾行? ? - (NSInteger)selectedRowInComponent:(NSInteger)component;
三、UIPickerView常用方法? ?
?1、dataSource方法? ? ?
?1)返回列數(shù)? ? ? ? - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView? ? ?
?2)返回每一列對應的行數(shù)? ? ? ? - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component? ?
?2、delegate方法? ? ? ??
1)返回顯示的文本? ? ? ? - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component? ? ? ?
?2)當某一列選中的行數(shù)發(fā)生變化時調(diào)用? ? ? ? ? - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component? ? ? ?
?3) 設置行高? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component? ? ? ??
4)設置列寬? ? ? ? - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component? ? ? ??
?5)自定義cell? ? ? ? - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
四、練習? ?
?1、使用拾取器做城市選擇? ? ??
1)在工程自帶的ViewController的viewDidload加入? ? ? ? UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-300, 414, 300)];? ? ? ? pickerView.backgroundColor = [UIColor grayColor];? ? ? ? ? ? [self.view addSubview:pickerView];? ? ? 運行,什么都沒有,因為多少段多少行是通過代理方法來設置的? ??
? 2)讓viewController類遵守兩個協(xié)議,在viewDidLoad方法里給pickerView掛上代理,并在viewController里實現(xiàn)如下兩個代理方法
//返回列數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//返回每一列中的行數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 5;
}
運行 選擇器里面有兩列,每列的數(shù)據(jù)都是用『?』表示,因為還沒有給每列賦值
3)將課件里面的city.plist文件拖到工程中,在viewDidLoad方法里寫入如下代碼
NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
//聲明一個全局變量dataArray
dataArray = [[NSArray alloc] initWithContentsOfFile:path];
4)將代理方法- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component的內(nèi)容替換成如下內(nèi)容
if (component == 0) {
return data.count;
}
NSInteger selectedRow =? [pickerView selectedRowInComponent:component];
NSArray *tempArray = data[selectedRow][@"cities"];
return tempArray.count;
運行 目前還是沒有數(shù)據(jù),但列數(shù)和行數(shù)已經(jīng)做了自動設置
5、給每行設置標題,將如下代理方法寫入工程
//返回每個item中的title
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {//第一列
NSDictionary *dic = data[row];
NSString *state = dic[@"state"];
return state;
}
//返回第一列選擇的行的索引
NSInteger selectedRow = [pickerView selectedRowInComponent:0];
//取得省級字典
NSDictionary *items = data[selectedRow];
//取得該省下所有的市
NSArray *cities = [items objectForKey:@"cities"];
NSString *cityName = cities[row];
return cityName;
}
運行 每行有標題 但左邊的省份變化,右邊的城市列表沒有跟著變化
6、實現(xiàn)右側(cè)跟著左側(cè)聯(lián)動
//當某一列選中的行數(shù)發(fā)生變化時調(diào)用
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
//刷新指定列中的行
[pickerView reloadComponent:1];
//選擇指定的item
[pickerView selectRow:0 inComponent:1 animated:YES];
}
}
運行 右側(cè)可以跟著左側(cè)聯(lián)動
7、設置列寬和行高,在工程里添加如下代理方法
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == 0) {
return 100;
}
return 220;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 30;
}
8、自定義cell,添加代理方法
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (component == 0) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor greenColor];
return view;
}
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 40)];
view2.backgroundColor = [UIColor redColor];
return view2;
}
運行 只剩色塊,而里面卻沒有內(nèi)容了,因為這個代理方法執(zhí)行- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component代理方法將不會再執(zhí)行