zoukankan      html  css  js  c++  java
  • UIPickerView 多级联动

    UIPickerView的多级联动问题有些地方需要注意,其实多级联动并不难。。。楼主因为项目需要又没法使用网上的第三方,所以最近写了一个省市区多级联动,还是手写代码好!!

    为了演示效果,我会多定义几个属性,表现演示效果

    @interface newTeamSettingController()<UIPickerViewDataSource,UIPickerViewDelegate>
    @property(strong,nonatomic)NSString* strWeek;//记录周数
    @property(strong,nonatomic)NSString* strTime;//记录时辰
    @property(strong,nonatomic)NSArray* timeArray;//数组保存
    @property(strong,nonatomic)UITextField* timeField;
    @end

    初始化,使textField一开始显示数据

    - (void)viewDidLoad {
    //初始化timeArray
        NSArray* weekArray=@[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
        NSArray* shijianArray=@[@"上午",@"下午",@"晚上"];
        self.timeArray=[NSArray arrayWithObjects:weekArray,shijianArray, nil];
     //请选择的时间textField
        self.timeField=[[UITextField alloc]initWithFrame:CGRectMake(470*luScaleW, 30*luScaleH, 192*luScaleW, 30*luScaleH)];
        self.timeField.tag=1;
        self.timeField.placeholder=@"请选择";
        self.timeField.textAlignment=NSTextAlignmentRight;
        self.timeField.adjustsFontSizeToFitWidth=YES;
        self.strWeek=[NSString stringWithFormat:@"%@",self.timeArray[0][0]];
        self.strTime=[NSString stringWithFormat:@"%@",self.timeArray[1][0]];
        [self.chuZhenView addSubview:self.timeField];
        //timePickerView
        self.timePickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.timeField.frame), LuW, 60)];
        self.timePickerView.tag=1;
        self.timePickerView.dataSource=self;
        self.timePickerView.delegate=self;
        self.timeField.delegate=self;
        self.timeField.inputView=self.timePickerView;
        //在这里设置下方数据刷新部分的初始显示
        for (int component = 0; component<self.timeArray.count; component++) {
               [self pickerView:nil didSelectRow:0 inComponent:component];
          }
         [self pickerView:nil didSelectRow:0 inComponent:0];
        self.doneBtn=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, LuW, 44)];
        UIBarButtonItem * button1 =[[UIBarButtonItem  alloc]initWithBarButtonSystemItem:                                        UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem* rightBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClick)];
        self.doneBtn.items=[NSArray arrayWithObjects:button1,rightBtn, nil];
        self.timeField.inputAccessoryView=self.doneBtn;
    }

    UIPickerView的代理方法

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return self.timeArray.count;      
    }
    //每列对应多少行
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
       NSArray* arryM=self.timeArray[component];
            return arryM.count;
    }
    //每行对应显示的数据是什么
    -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
      NSArray* arrayM=self.timeArray[component];
            NSString* name=arrayM[row];
            return name;
    }
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
      if (component==0) {
                [pickerView reloadComponent:1];
                [pickerView selectRow:0 inComponent:1 animated:YES];
                _strWeek=self.timeArray[component][row];
                _strTime=self.timeArray[1][0];
            }
            else
            {
                _strTime=self.timeArray[component][row];
            }
            self.timeField.text=[NSString stringWithFormat:@"%@ %@",_strWeek,_strTime];
    }

    完成。

  • 相关阅读:
    漏斗算法 java
    servlet request参数只能取一次解决方法
    redis的使用
    关于Http协议与TCP协议的一些简单理解
    OSI七层与TCP/IP五层网络架构详解
    linux命令详解——eval
    使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
    hadoop相关随记
    mesos-master启动失败,报错Failed to load unknown flag 'quorum.rpmsave'
    根目录/缺少执行权限x产生的两种错误
  • 原文地址:https://www.cnblogs.com/luerniu/p/4752076.html
Copyright © 2011-2022 走看看