UIPickerView

一、UIPickerView常用方法

  1、UIPickerView和UIDatePicker是类似的控件,只不过UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何东西。

  2、UIPickerView例子

  

  3、UIPickerView代理

    @property(nonatomic,assign)

      id<UIPickerViewDelegate> delegate;

    @property(nonatomic,assign)

      id<UIPickerViewDataSource>dataSource;

    delegate 定义了UIPickerView的外观和属性

    dataSource 定义了UIPickerView的数据源和定制内容

  4、UIPickerView常用方法

    1>返回component列,row行的一个UIView,这里只有在定制的情况下才有小,其他情况返回nil

    2>- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

    3>重新装在整个UIPickerView所有列的数据和指定列的数据

    4>- (void) reloadAlllComponents;

    5>- (void) reloadComponent:(NSInteger)component;

    6>现在UIPickerView中component列,row行,也就是让改行滚动到中央

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

    8>返回定制列component中选中的行,没有选中返回-1;

    9>- (NSInteger)selectedRowInComponent:(NSInteger)component;

二、UIPickerViewDataSource

  1、返回UIPickerView一共有几列

    - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView;

  2、返回定制的component列有几行数据

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

三、UIPickerViewDelegate

  1、

    

  2、

     

  3、主要的使用的借个方法的代码如下

    

View Code
#pragma mark-
#pragma mark PickerView function

/*表示UIPickerView一共有几列*/
- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
/*返回component这列有多少行数据*/
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [fonts count];
}
/*返回component这列 row这行里面的字符串是什么*/
- (NSString *) pickerView:(UIPickerView *)pickerView 
              titleForRow:(NSInteger)row 
             forComponent:(NSInteger)component
{
    return [fonts objectAtIndex:row];
}
/*当我们选择UIPickerView中列为component,行为row的回调函数*/
- (void) pickerView:(UIPickerView *)pickerView 
       didSelectRow:(NSInteger)row 
        inComponent:(NSInteger)component
{
    /*设置字体的样式*/
    NSLog(@"font %d is selected",row);
    NSString *fontName=[fonts objectAtIndex:row];
    fontLabel.font=[UIFont fontWithName:fontName size:20.0f];
    fontLabel.text=[NSString stringWithFormat:@"字体 %@ 选中了",fontName];
}

四、定制UIPickerView

  1、

        

  

    

 
 
分类: IOS
原文地址:https://www.cnblogs.com/Leo_wl/p/2843702.html