天天看點

17.picker view的基本使用

//在picker view的練習國旗的選擇中
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    NSLog(@"%p",view);
    ContryView *view1=(ContryView *)view;

    //該view的重用在iOS7有bug
    if (!view1) {
        view1=[ContryView getCountryView];
    }
    view1.country=self.contrys[row];
    return view1;
}

可以自定義一個view類利用xib生産view ,設定一個model資料源後傳回一個view給picker view即可.


//該方法可以設定picker view的每列的每行的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return ;
}
           
在省份選擇的練習中,第二行的城市是根據第一行的狀态選擇的,先确定第一行的選擇是什麼,然後加載第二行的資料
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    Shengfen *shenfen=self.shengfen[component];
    if (component==) {
        return self.shengfen.count;
    }else{
        NSInteger select=[pickerView selectedRowInComponent:];
        Shengfen *shenfen1=self.shengfen[select];

    return shenfen1.cities.count;
    }
}


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

    if (component==) {
        Shengfen *shenfen=self.shengfen[row];
        return shenfen.name;
    }else{

        NSInteger select=[pickerView selectedRowInComponent:];
        Shengfen *shenfen=self.shengfen[select];

      return shenfen.cities[row];

    }
}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==) {
        [pickerView reloadComponent:];
        //第一列選擇之後,立刻跳到第二列的第一行
        [pickerView selectRow: inComponent: animated:YES];
    }
    NSInteger select=[pickerView selectedRowInComponent:];
    Shengfen *shenfen=self.shengfen[select];
    NSString *str=nil;
    if (component==) {
       str= shenfen.cities[row];
    }
    NSString *str1=[NSString stringWithFormat:@"%@  %@",shenfen.name,str];
    self.label.text=str1;
}

           

繼續閱讀