IOS拾取器piker依赖组件实例

作者:朱克锋

邮箱:zhukefeng@iboxpay.com

转载请注明出处:http://blog.youkuaiyun.com/linux_zkf

pikerIOS开发中有着广泛的应用,这里实现的是依赖组件的piker,实现piker需要遵从piker的数据源与代理协议

UIPickerViewDelegate, UIPickerViewDataSource

示例效果如图


头文件

#import <UIKit/UIKit.h>

#define F_C   0

#define S_C   1

@interface DependentComponentPickerViewController : UIViewController

<UIPickerViewDelegate, UIPickerViewDataSource> {

    UIPickerView    *picker;

    NSDictionary    *cityCodes;

    NSArray         *citys;

    NSArray         *codes;

}

@property (retain, nonatomic) IBOutlet UIPickerView *picker;

@property (retain, nonatomic) NSDictionary *cityCodes;

@property (retain, nonatomic) NSArray *citys;

@property (retain, nonatomic) NSArray *codes;

- (IBAction) buttonPressed;

@end


实现文件

#import "DependentComponentPickerViewController.h"

@implementation DependentComponentPickerViewController

@synthesize picker;

@synthesize cityCodes;

@synthesize citys;

@synthesize codes;

- (IBAction) buttonPressed {

    NSInteger cityRow = [picker selectedRowInComponent:F_C];

    NSInteger codeRow = [picker selectedRowInComponent:S_C];

    NSString *city = [self.citys objectAtIndex:cityRow];

    NSString *code = [self.codes objectAtIndex:codeRow];

//.……

}

- (void)viewDidLoad {

    NSBundle *bundle = [NSBundle mainBundle];

    NSString *plistPath = [bundle pathForResource:

                           @"statedictionary" ofType:@"plist"];

    NSDictionary *dictionary = [[NSDictionary alloc]

                                initWithContentsOfFile:plistPath];

    self.cityCodes = dictionary;

    [dictionary release];

    NSArray *components = [self.cityCodes allKeys];

    NSArray *sorted = [components sortedArrayUsingSelector:

                       @selector(compare:)];

    self.citys = sorted;

    NSString *selectedState = [self.citys objectAtIndex:0];

    NSArray *array = [cityCodes objectForKey:selectedState];

    self.codes = array;

}

#pragma mark -

#pragma mark Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

    return 2;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView

numberOfRowsInComponent:(NSInteger)component {

    if (component == F_C)

        return [self.citys count];

    return [self.codes count];

}

#pragma mark Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView

titleForRow:(NSInteger)row

forComponent:(NSInteger)component {

    if (component == F_C)

        return [self.citys objectAtIndex:row];

    return [self.codes objectAtIndex:row];

}

- (CGFloat)pickerView:(UIPickerView *)pickerView

    widthForComponent:(NSInteger)component {

    if (component == S_C)

        return 90;

    return 200;

}

- (void)pickerView:(UIPickerView *)pickerView

  didSelectRow:(NSInteger)row

       inComponent:(NSInteger)component {

    if (component == F_C) {

        NSString *selectedCity = [self.citys objectAtIndex:row];

        NSArray *array = [cityCodes objectForKey:selectedCity];

        self.codes = array;

        [picker selectRow:0 inComponent:S_C animated:YES];

        [picker reloadComponent:S_C];

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值