UIKit 框架之UIPickerView

本文介绍了一个简单的 iOS UIPickerView 示例实现,展示了如何使用 UIPickerView 来显示省份和城市的数据,并提供了 UIPickerView 的数据源和代理方法的具体实现。
//
//  ViewController.m
//  UIPickerView
//
//  Created by City--Online on 15/5/18.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong) UIPickerView *pickerView;
@property(nonatomic,strong) NSMutableArray *provinceArr;
@property(nonatomic,strong) NSMutableArray *cityArr; //省份对应下的城市及显示的城市
@property(nonatomic,strong) NSDictionary *dicData;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _pickerView=[[UIPickerView alloc]init];
    CGSize size=[UIScreen mainScreen].bounds.size;
    _pickerView.frame=CGRectMake(10, 10, size.width, 100);
    _pickerView.showsSelectionIndicator=YES;
    _pickerView.dataSource=self;
    _pickerView.delegate=self;
    [self.view addSubview:_pickerView];
    
    _provinceArr=[[NSMutableArray alloc]initWithObjects:@"河南",@"广东", nil];
    NSArray *arr1=[[NSArray alloc]initWithObjects:@"----",@"驻马店",@"周口",nil];
    NSArray *arr2=[[NSArray alloc]initWithObjects:@"----",@"深圳",@"广州",@"东莞", nil];
    _dicData=[[NSDictionary alloc]initWithObjects:[[NSArray alloc] initWithObjects:arr1,arr2, nil] forKeys:_provinceArr];
    
    //找到第一个轮子选择行的索引
    NSInteger selectProvinceIndex=[_pickerView selectedRowInComponent:0];
    //根据索引找到对应的省份
    NSString *province=[[_dicData allKeys]objectAtIndex:selectProvinceIndex];
    //根据省份找到对应的城市
    _cityArr=[[NSMutableArray alloc]init];
    _cityArr=[_dicData objectForKey:province];
    
    NSLog(@"%@",_dicData);
    
    
}
//UIPickerViewDataSource
//列个数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    NSLog(@"allKeys=%ld",[_dicData allKeys].count);
    return _provinceArr.count;
    
}
//行个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return _provinceArr.count;
    }
    else
    {
        return _cityArr.count;
    }
    
}
//UIPickerViewDelegate
//每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return _pickerView.frame.size.width/2;
}
//每列中行的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if (component==0) {
        return 50;
    }
    return 30;
}
//填充每行内容
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
//{
//    if (component==0) {
//        return [_provinceArr objectAtIndex:row];
//    }
//    else{
//        return [_cityArr objectAtIndex:row];
//    }
//}


//为没行设置文本设置属性
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str=[[NSString alloc]init];
    if (component==0) {
        str=[_provinceArr objectAtIndex:row];
    }
    else{
        str=[_cityArr objectAtIndex:row];
    }
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:str];
    UIFont *font = [UIFont systemFontOfSize:10];
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, str.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, str.length)];
    return attributedString;
    
}
//当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view  可以自定义View
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//    UILabel *label=[[UILabel alloc]init];
//    label.text=@"text";
//    label.backgroundColor=[UIColor redColor];
//    return label;
//    
//}

//选中某一轮子的某一列
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        NSString *selectProvince=[_provinceArr objectAtIndex:row];
        _cityArr=[_dicData objectForKey:selectProvince];
        //重新加载某一个轮子
        [_pickerView reloadComponent:1];
        //选中轮子的某一行
        [_pickerView selectRow:0 inComponent:1 animated:YES];
        
    }
    else
    {
        if (row==0) {
            return;
        }
        NSInteger selectProvinceIndex=[_pickerView selectedRowInComponent:0];
        NSString *province=[_provinceArr objectAtIndex:selectProvinceIndex];
        NSString *city=[_cityArr objectAtIndex:row];
        NSLog(@"%@-----%@",province,city);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

转载于:https://www.cnblogs.com/5ishare/p/4511976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值