第十章 UIPickerView

UIPickerView在什么时候用?

  • 通常在注册的时候选择一个城市时候调用

我们学习一个控件可以首先进入他的头文件,在UIPickerView的头文件我们发现他有两个代理

  • UIPickerViewDataSource
/**
 *  返回控件一共有多少列
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 4;
}
/**
 *  返回控件component列有多少行
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        if (component == 0) {// 第0列
        return array.count;
    } else if (component == 1) {
        // 当选中第0列的某一行的时候返回的下表,我们可以用来做联动效果。
        // 例如当又一个plist问价,根节点是数组,数组里面是字典,字典包含的是一个字符串一个数组,我们可以用字符串代表第0列,数组代表选中的字符串后的第二列的行数
        // 例如我们选中的是一个中国省份城市的plist,字符串代表省份,数组代表每个省的城市
        NSInteger index = [pickerView selectedRowInComponent:0];
        JXModel * model = array[index];
        return model.arrar.count
    }
    return 0;
}
  • UIPickerViewDelegate
/**
 *  第component列的宽度
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 100;
}
/**
 *  第component列每行的高度
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 60;
}
/**
 *  第component列的row行的标题,文字标题
 *  @param row        行
 *  @param component  列
 *
 *  @return 文字标题
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return @"iOS学习";
}
/**
 *  设置标题富文本
 *  @param row        行
 *  @param component  列
 */
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    dict[NSForegroundColorAttributeName] = [UIColor redColor];
    NSAttributedString * string = [[NSAttributedString alloc] initWithString:@"这是学习IOS" attributes:dict];
    return string;
}
/**
 *  返回UIView
 *  @param row        行
 *  @param component  列
 *  @param view       UIview
 */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    return [UIButton buttonWithType:UIButtonTypeContactAdd];
}
/**
 *  选中的标签
 *  @param row        行
 *  @param component  列
 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"%zd---%zd",row,component);
}

results matching ""

    No results matching ""