天天看點

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