UIKit 框架之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

 

时间: 2024-11-03 17:54:10

UIKit 框架之UIPickerView的相关文章

iOS UIKit 框架 346 篇文档分类整理 - 预告

iOS UIKit 框架 346 篇文档分类整理 - 预告 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分

UIKit 框架之UIView二

下面这些都是UIView一些基本的东西,具体的可以参考UIKit 框架之UIView一博客 一.自定义一个View // // MyView.m // UIView // // Created by cyw on 15-5-17. // Copyright (c) 2015年 cyw. All rights reserved. // #import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame {

UIKit 框架之Bar、Controller

UIKit框架中有各种Bar,UITabBar.UINavigationBar.UIToolbar.Bar对应的就有一些Item,tabBarItem.navigationItem.toolbarItems,再加上UIViewController.UINavigationController.UITabBarController很容易搞糊涂.我看了好久,没看明白.动手敲了下才有一点感觉. 一.联系 一个UINavigationController对应着一个UINavigationBar.UITo

UIKit框架类层次图

学习UIKit应该首选了解UIKit类的层次图,从根类一层一层的拨.

UIKit 框架之UICollectionViewController

1.自定义单元格 #import <UIKit/UIKit.h> @interface myCollectionViewCell : UICollectionViewCell @property(nonatomic,strong) UIImageView *myImageView; @property(nonatomic,strong) UILabel *nameLabel; @end #import "myCollectionViewCell.h" @implementa

UIKit 框架之UIView一

- (id)initWithFrame:(CGRect)aRect //通过一个矩形对象初始化 Configuring a View's Visual Appearance //配置视觉展示 @property(nonatomic, copy) UIColor *backgroundColor //设置背景色 @property(nonatomic, getter=isHidden) BOOL hidden //隐藏view,默认为NO @property(nonatomic) CGFloat

UIKit 框架之UICollectionView

1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectionView // // Created by City--Online on 15/5/25. // Copyright (c) 2015年 XQB. All rights reserved. // #import <UIKit/UIKit.h> @interface myCollectionVi

UIKit框架之NSObject

首先学习NSObject  // // ViewController.m // localization // // Created by City--Online on 15/5/15. // Copyright (c) 2015年 XQB. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (voi

UIKit 框架之UIScrollView

理解UIScrollView时可以假设这样的一个场景:在一个漆黑的屋子里的墙上有一副大的画,墙是可以上下左右滚动,一个矩形的手电筒固定着,开始时画的左上角坐标与矩形光的左上角坐标对应.人可以用手电筒的光来看这幅画. // // ViewController.m // UIScrollView // // Created by City--Online on 15/5/20. // Copyright (c) 2015年 XQB. All rights reserved. // #import "