美文网首页iOS学习iOS UIKitUIVIew
UIKit-UIPickerView详解

UIKit-UIPickerView详解

作者: 才寒 | 来源:发表于2015-03-03 17:51 被阅读6998次
    UIPickerView继承图

    1.UIPickerView的创建

    必须实现委托delegate和数据源dataSource

    UIPickerView的高度只能为三种值:

    height设置区间在0~179时,UIPickerView的height为162

    height设置区间在180~215时,UIPickerView的height为180

    height设置区间在216~∞时,UIPickerView的height为216

    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, 300, 180)];

    pickerView.dataSource = self;

    pickerView.delegate = self;

    2.showsSelectionIndicator    设置没设置都是一个样,无聊的可以试试

    @property(nonatomic)   BOOL  showsSelectionIndicator;// default is NO

    3.numberOfComponents    获取选择器的分组数

    @property(nonatomic,readonly)   NSInteger  numberOfComponents;

    4.UIPickerView的方法

    //获取指定分组component中的行数

    - (NSInteger)numberOfRowsInComponent:(NSInteger)component;

    //获取单元格的Size

    - (CGSize)rowSizeForComponent:(NSInteger)component;

    //获取pickerView:viewForRow:forComponent:reusingView:中定义的View,当pickerView:viewForRow:forComponent:reusingView:未实现或者行或分组不可用时返回nil

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

    //刷新所有分组

    - (void)reloadAllComponents;

    //刷新指定分组

    - (void)reloadComponent:(NSInteger)component;

    //选中指定分组中的一行

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

    //获得指定分组中选中的行

    - (NSInteger)selectedRowInComponent:(NSInteger)component;

    5.数据源UIPickerViewDataSource

    @protocol UIPickerViewDataSource

    @required

    //设置分组数

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

    // 设置各分组的行数

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

    @end

    6.委托UIPickerViewDelegate

    // 设置分组的宽

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

    //设置单元格的高

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

    单元格中内容的展示,以下三种方法三选一

    //设置显示普通字符串

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

    //设置显示属性字符串,如果该方法和普通字符串方法都实现了,效果是属性字符串

    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0);

    //自定义显示视图

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

    单元格选中时的委托方法,需要注意的是,单元格的值改变后停止滚动动画停止即调用这个方法,不需要手点击

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

    7.效果图

    相关文章

      网友评论

      本文标题:UIKit-UIPickerView详解

      本文链接:https://www.haomeiwen.com/subject/jcsmxttx.html