码迷,mamicode.com
首页 > 其他好文 > 详细

UIPicker city.plist(实例)

时间:2016-03-11 23:51:29      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

前言: UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。UIPickerView 直接继承了 UIView ,没有继承 UIControl ,因此,它不能像 UIControl 那样绑定事件处理方法, UIPickerView 的事件处理由其委托对象完成。

正文: UIPickerView 控件常用的属性和方法如下:

  numberOfComponents: 获取UIPickerView指定列中包含的列表项的数量。该属性是一个只读属性。 

  showsSelectionIndicator: 该属性控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)。 

  numberOfRowsInComponent: 获取UIPickerView包含的列数量。 

  rowSizeForComponent: 获取UIPickerView包含的指定列中列表项的大小。该方法返回一个CGSize对象。 

  selectRow:inComponent:animated:: 该方法设置选中该UIPickerView中指定列的特定列表项。最后一个参数控制是否使用动画。 

  selectedRowInComponent:: 该方法返回该UIPickerView指定列中被选中的列表项。 

  viewForRow:forComponent:: 该方法返回该UIPickerView指定列的列表项所使用的UIView控件。 

UIDatePicker控件只是负责该控件的通用行为,而该控件包含多少列,各列包含多少个列表项则由UIPickerViewDataSource对象负责。开发者必须为UIPickerView设置UIPickerViewDataSource对象,并实现如下两个方法。 

  numberOfComponentsInPickerView:: 该UIPickerView将通过该方法来判断应该包含多少列。 

  pickerView:numberOfRowsInComponent:: 该UIPickerView将通过该方法判断指定列应该包含多少个列表项。 

如果程序需要控制UIPickerView中各列的宽度,以及各列中列表项的大小和外观,或程序需要为UIPickerView的选中事件提供响应,都需要为UIPickerView设置UIPickerViewDelegate委托对象,并根据需要实现该委托对象中的如下方法。 

  pickerView:rowHeightForComponent:: 该方法返回的CGFloat值将作为该UIPickerView控件中指定列中列表项的高度。 

  pickerView:widthForComponent:: 该方法返回的CGFloat值将作为该UIPickerView控件中指定列的宽度。 

  pickerView:titleForRow:forComponent:: 该方法返回的NSString值将作为该UIPickerView控件中指定列的列表项的文本标题。 

  pickerView:viewForRow:forComponent:reusingView:: 该方法返回的UIView控件将直接作为该UIPickerView控件中指定列的指定列表项。 

  pickerView:didSelectRow:inComponent:: 当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法 

Interface Builder只支持为UIPickerView设置一个属性——Shows Selection Indicator,该属性用于控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)。

 
 
@property(strong,nonatomic) UIPickerView *picker;
@property(strong,nonatomic) NSMutableArray *sheng;
@property(strong,nonatomic) NSMutableArray *shi;
@property(strong,nonatomic) NSArray *array;
 
 

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sheng=[NSMutableArray array];
    self.shi=[NSMutableArray array];
    NSBundle *bundle=[NSBundle mainBundle];
    NSString *path=[bundle pathForResource:@"city.plist" ofType:nil];
    self.array=[NSArray arrayWithContentsOfFile:path];
 
 
    for (NSDictionary *ar1 in self.array) {
 
 
        NSArray *ar2=[ar1 objectForKey:@"Cities"];
        [self.sheng addObject:[ar1 objectForKey:@"State"]];
        for (NSDictionary *ar3 in ar2) {
//            NSLog(@"%@",[ar3 objectForKey:@"city"]);
        [self.shi addObject:[ar3 objectForKey:@"city"]];
           
        }
       
       
    }

    self.picker=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 200, 414, 300)];
    self.picker.backgroundColor=[UIColor greenColor];
   
    [self.view addSubview: self.picker];
    self.picker.delegate=self;
    self.picker.dataSource=self;
   
}

#pragma mark -数据源  nubertOfComponentsInPickerView:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

#pragma mark - 数据源 pickerView: attributedTitleForRow: forComponent:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
   
 
   
    if (component==0) {
        return self.sheng.count;
    }
         return self.shi.count;
}


#pragma mark - 显示信息方法  delegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return self.sheng[row];
       
    }
   
  
         return self.shi[row];
}
#pragma mark -选中行的信息
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        //清除上次选择地城市
        [self.shi removeAllObjects];
        NSDictionary *dic=[self.array objectAtIndex:row];
//          NSLog(@"%@",dic);
        NSArray *arr1=dic[@"Cities"];
     

        NSMutableArray * arr2 =[NSMutableArray array];
        for (NSDictionary * dic in arr1) {
            [arr2 addObject:dic[@"city"]];
           
          
        }
        self.shi=arr2;
        [self.picker selectRow:row inComponent:0 animated:YES];
       
        [self.picker reloadComponent:1];
       
        }
    else {
        NSInteger firstRow=[self.picker selectedRowInComponent: 0];
        NSInteger secondRow=[self.picker selectedRowInComponent:1];
       
        NSString *firstString=[self.sheng objectAtIndex:firstRow];
        NSString *secondString=[self.shi objectAtIndex:secondRow];
       
       
        NSString *message=[NSString stringWithFormat:@"您确定要选择%@%@吗?",firstString,secondString];
        UIAlertController *alertMessage=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
       
        UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
       
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
       
        [alertMessage addAction:cancle];
        [alertMessage addAction:ok];
        [self presentViewController:alertMessage animated:YES completion:nil];
        NSLog(@"%@%@",firstString,secondString);

    }
   
       
    }

   

#pragma mark - 行高
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if (component==0) {
         return  80;
    }
    return 50;
}

UIPicker city.plist(实例)

标签:

原文地址:http://www.cnblogs.com/tianlianghong/p/5267548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!