UIPickerView的使用

DataSource协议

必须要实现这两个方法

    // 返回pickerView有多少列
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foods.count;
}
    // 返回第component列有多少行
    - (NSInteger)r :(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.foods[component] count];
}

Delegate协议

常用的几种方法

// 返回第component列的每一行的行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 80.0;
}

// 返回第component列第row行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return @"aaaaa";
}

// 返回第component列第row行的View
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];

    v.backgroundColor = [UIColor redColor];

    return v;
}

// 选中第component第row的时候调用
// 注意:这个方法必须用户主动拖动pickerView,才会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%s---%ld-%ld",__func__,component,row);
}
原文地址:https://www.cnblogs.com/luoze/p/5468168.html