• 选择器(UIPickerView)


    Apple提供了两种形式的选择器:日期选择器(UIDatePicker)与自定义选择器(UIPickerView)。

    当用户使用日期选择器选定一个时间后,调用datePickerID.date可以直接获得一个NSDate对象,从而可以进行一系列的时间与日期处理。

    自定义选择器的使用稍微麻烦点,当你直接从控件栏里拖放Picker View到故事板然后运行,会发现调试器界面空无一物。这是因为UIPickerView必须用代码实现一些协议才能正常显示。而且无法在Attributes Inspector中配置选择器视图的外观。

    自定义选择器需要遵守两个协议:数据源协议和委托协议。

    1. 数据源协议

    数据源协议(UIPickerViewDataSource)用来描述选择器显示多少信息。有下面两个主要方法:
    numberOfComponentInPickerView -- 返回选择器的组件数
    pickerView:numberOfRowsInComponent -- 返回指定组件包含多少行(不同的输入值)

    例如,如果要创建一个选择器,显示两列,其中第一列包含一个可供选择的值,而第二列包含两个,则代码实现如下:

     @interface ViewController : UIViewController <UIPickerViewDataSource>
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 2; //显示两列
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        if(component==0)
        {
            //如果是第一列,显示1个标签
            return 1;
        }
        else
        {
            //否则显示2个标签
            return 2;
        }
    }

    2. 委托协议

    委托协议(UIPickerViewDelegate)负责创建和使用选择器的系列工作。它负责将合适的数据传递给选择器进行显示,并确定用户是否做出了选择。该协议包含多个方法,但只有两个是必不可少的:
    pickerView:titleForRow:forComponent -- 根据指定的组件和行号返回该行的标题,即应向用户显示的字符串
    pickerView:didSelectRow:inComponent -- 当用户在选择器中做出选择时,将调用该委托方法,并向它传递用户选择的行号以及用户最后触摸的组件

    UIPickView还有一个实例方法- (NSInteger)selectedRowInComponent:(NSInteger)component,用来返回指定列当前选择的行数。通常和pickerView:didSelectRow:inComponent配合使用。想象这样的情况:选择器有两列,第一列是国家,第二列是地区。假如你先转动了国家,这个时候你想得到默认选择的地区,就只能通过selectedRowInComponent来取得。(这个时候didSelectRow得到的row值,是国家那一列的)

    @interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        if(component==0)
        {
            //如果是第一列,显示"中国"
            return @"中国";
        }
        else
        {
            if(row==0)
            {
                return @"北京";
            }
            else
            {
                return @"香港";
            }
        }
    }
    
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
        if(component==0)
        {
            NSLog(@"用户选择了第一列");
        }
        else
        {
            NSLog(@"用户选择了第二列");
    
            NSLog(@"用户选择了第%d行",row);
        }
    }

    还有一些非必要,但是也很有用的方法:
    pickerView:rowHeightForComponent -- 给指定组件返回(设置)行高,单位为像素
    pickerView:widthForComponent -- 给指定组件返回(设置)宽度,单位为像素
    pickerView:viewForRow:viewForComponent:ReusingView -- 给指定组件和行号返回相应位置应显示的自定义视图,优先级高于pickerView:titleForRow:forComponent

    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
    {
        if(component==0)
        {
            //设置第一列的行高
            return 50;
        }
        else
        {
            return 30;
        }
    }
    
    - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
    {
        if(component==0)
        {
            //设置第一列的宽度
            return 100;
        }
        else
        {
            return 80;
        }
    }
    
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        if(component==0)
        {
            //第一列返回一个Label组件
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 32)];
            label.backgroundColor = [UIColor greenColor];
            label.textColor = [UIColor redColor];
            label.text = @"First";
            return label;
        }
        else
        {
            //第二列返回一个图片组件
            if(row==0)
            {
                return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Orange.gif"]];
            }
            else
            {
                return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Strawberry.gif"]];
            }
        }
    }

    UIDatePicker有一个实例方法setDate:animated,以编程的方式选择日期。

    UIPickerView有一个实例方法selectRow:inComponent:animated,以编程的方式来选择值。

  • 相关阅读:
    记一次排查tomcat耗费CPU过高的经历
    记录一次PHP项目报502的问题
    工作职责与工作协调
    如何提升团队效率
    接手老项目的方法总结
    如何快速确定需求的技术实现方案
    PHP中的赋值-引用or传值?
    性能优化-开发流程中的性能优化
    一周阅读列表(20131111-20131117)
    一周阅读列表(20131104-20131110)
  • 原文地址:https://www.cnblogs.com/CoderWayne/p/3596797.html
Copyright © 2020-2023  润新知