UIPickerView 实现多列选择栏

来源:互联网 发布:佣兵天下的java游戏 编辑:程序博客网 时间:2024/05/01 09:06

首先要实现以下两个代理:UIPickerViewDelegate、UIPickerViewDatasource

UIPickerViewDelegate中的方法:

– pickerView:rowHeightForComponent:
– pickerView:widthForComponent:
– pickerView:titleForRow:forComponent:
– pickerView:viewForRow:forComponent:reusingView:
– pickerView:didSelectRow:inComponent:


UIPickerViewDatasource中的方法:

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

 

下面是一些儿实例中的代码

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

//用于设置pickerView多少列选择栏
- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component{
    if (component == PickerOne) {
        return [array1 count];
    }
    return [array2 count];
}

//设置每一列有多少个行数 


- (NSString *)pickerView:(UIPickerView *)pickerView 
             titleForRow:(NSInteger)row 
            forComponent:(NSInteger)component{
    if (component == PickerOne) {
        return  [self.array1 objectAtIndex:row];
    }
    NSArray * array=[self.dictionary2 allValues];
    array=[array sortedArrayUsingSelector:@selector(compare:)];
    return [self.dictionary2 valueForKey:[array2 objectAtIndex:row]];
}

//设置每列数据 


- (CGFloat)pickerView:(UIPickerView *)pickerView 
    widthForComponent:(NSInteger)component{
    if (component==0) {
        return 120;
    }
    return 200;
}

//设置每列的宽度 


- (void)pickerView:(UIPickerView *)pickerView 
      didSelectRow:(NSInteger)row 
       inComponent:(NSInteger)component{
    if (component==0) {
        self.UMengFeedbackGender=row;
    }
    if (component==1) {
        self.UMengFeedbackAge=row;
    }
    [self showButtonText:[self.dictionary1 valueForKey:[NSString 
                                                        stringWithFormat:@"%d",
                                                        self.UMengFeedbackGender]]
               stringAge:[self.dictionary2 valueForKey:[NSString 
                                                        stringWithFormat:@"%d",
                                                        self.UMengFeedbackAge]]];
}

//设置选中一行后触发的事件