天天看点

UIPickerView简介

UIPickerView是选择器类,类似于老虎机或者轮盘来表示一个或多个值的集合。用户可以滑动,来选择所需的值。

获取视图选择器的尺寸:

1.返回UIPickerView当前的列数:

@property(nonatomic, readonly) NSInteger numberOfComponents
           

2.返回指定列的行数:

- (NSInteger)numberOfRowsInComponent:(NSInteger)component
           

3.返回每一行的尺寸:

- (CGSize)rowSizeForComponent:(NSInteger)component
           

加载视图选择器:

4.重新载入该选择器视图的所有组件:

- (void)reloadAllComponents
           

5.重新加载某一列的组件:

- (void)reloadComponent:(NSInteger)component
           

行选择:

6.选择指定列的指定行:

- (void)selectRow:(NSInteger)row
      inComponent:(NSInteger)component
         animated:(BOOL)animated
           

7.第几列被选中的行号:

- (NSInteger)selectedRowInComponent:(NSInteger)component
           

返回行和列的视图:

8.返回指定行和列的视图:

- (UIView *)viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           

指定代理:

@property(nonatomic, assign) id< UIPickerViewDelegate > delegate
           

指定数据源:

@property(nonatomic, assign) id< UIPickerViewDataSource > dataSource
           

外观管理:

显示选中框:

@property(nonatomic) BOOL showsSelectionIndicator
           

选择器要显示数据,需要依赖两个协议:UIPickerViewDelegate和UIPickerViewDataSource。

UIPickerViewDelegate:

设置选择器视图的尺寸:

1.设置行高:

- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component
           

2.设置行宽:

- (CGFloat)pickerView:(UIPickerView *)pickerView
    widthForComponent:(NSInteger)component
           

设置每行的内容:

3.返回当前行的内容,此处是将内容添加到滚动的那个显示栏上:

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
           

例如:

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
{  
    return [pickerData objectAtIndex:row];  
}  
           

4.

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView
             attributedTitleForRow:(NSInteger)row
                      forComponent:(NSInteger)component
           

5.当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view:

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view
           

选择响应行:

6.当用户选择某个row时,picker view调用此函数:

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
           

UIPickerViewDataSource:

为选择器提供计数:

1.返回应该有几列:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
           

2.返回当前列显示的行数:

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component