(UIPickerView)选择器的使用--相互依赖的多列选择器

首先建立工程,然后再stroyboard拖入UIPicker,为在程序中访问该控件,需要将该控件绑定到picker IBOutlet属性

然后实现两个协议,这两个协议是必须存在的。代码如下

#import "ViewController.h"

@interface ViewController ()


@property (strong, nonatomic) IBOutlet UIPickerView *picker;

@end

@implementation ViewController
{
    NSDictionary *books;
    NSArray *authors;
    //selectedAuthor保存当前选中的作者
    NSString *selectAuthor;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建并初始化NSDictionary对象
    books = [NSDictionary dictionaryWithObjectsAndKeys:
             [NSArray arrayWithObjects:@"飞鸟集",@"祭坛急了", nil],@"泰戈尔",
             [NSArray arrayWithObjects:@"醒世恒言",@"喻世明言",@"警世通言", nil],@"冯梦龙",
             [NSArray arrayWithObjects:@"ios讲义",@"andros讲义",@"java讲义",@"xml讲义", nil],@"李刚",nil];
    //使用author 保存books所有key所组成的NSArray排序后的结果
    authors = [[books allKeys] sortedArrayUsingSelector:@selector(compare:)];
    //使用默认选中的作者authors中的第一个元素
    selectAuthor = [authors objectAtIndex:0];
    
    self.picker.delegate = self;
    self.picker.dataSource = self;
    
}
//该方法返回控件该控件包含多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//该方法返回值决定该控件的制定列包含多少个列表项
-(NSInteger)pickerView:(UIPickerView *)pickerView                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return authors.count;
    }
    return [[books objectForKey:selectAuthor] count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //若果是第一列,则返回authors中的元素的个数
    //即authors有多少个元素,那么第一列就包含多少个列表项
    if (component == 0) {
        return [authors objectAtIndex:row];
    }
    //否则其他列返回的个数是books中的键值对中值对应的数组地元素的个数
    return [[books objectForKey:selectAuthor] objectAtIndex:row];
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
     //如果是第一列,则宽度是90
    if (component == 0  ) {
        return 90;
    }
    return 210;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
    if (component == 0) {
        //改变选中的作者
        selectAuthor = [authors objectAtIndex:row];
    }
    //控制重写加载第二个列表,根据选中的作者来加载第二个列表项
    [self.picker reloadComponent:1];
}

@end

(UIPickerView)选择器的使用--相互依赖的多列选择器_第1张图片

运用下面该方法可以任意定制自己的的选择器视图

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"引导页二640-1136"]];
    return view;
}

如图所示

(UIPickerView)选择器的使用--相互依赖的多列选择器_第2张图片






1



点击打开链接






你可能感兴趣的:(iOS,iOS研发进阶之路)