UIPickerView

UIPickerView

效果图:

/** 数据源 */

@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;                // 默认为nil,弱引用

/** 代理 */

@property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;                  // 默认为nil,弱引用

/** 显示选择标示 */

@property(nonatomic)        BOOL                       showsSelectionIndicator;   // 默认为NO


// 列数

@property(nonatomic,readonly) NSInteger numberOfComponents;

// component列中的行数

- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

// 指定列中每一行的size

- (CGSize)rowSizeForComponent:(NSInteger)component;


// 返回由代理方法pickerView:viewForRow:forComponent:reusingView:提供的视图

// 如果没有实现该代理方法,或者component列的row行不可见,则返回nil

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;


// 重新加载所有列的数据

- (void)reloadAllComponents;

// 重新加载指定列的数据

- (void)reloadComponent:(NSInteger)component;


// 选中指定component列中的row行,不会触发代理方法

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;  // 滚动指定的行到中间位置


// 返回当前选中的行,如果没有选中行,返回-1

- (NSInteger)selectedRowInComponent:(NSInteger)component;


@end


/** 数据源协议,所有方法都必须实现 */

@protocol UIPickerViewDataSource<NSObject>

@required


// 返回要显示的列数

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


// 返回component列中的数据行数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

@end


/** 代理协议,所有方法都是可选的 */

@protocol UIPickerViewDelegate<NSObject>

@optional


// 返回指定列的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

// 返回指定列的行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;


/**

 以下三个返回指定列对应行的内容

 

 可以是:

 1> NSString

 2> NSAttributedString

    如果同时实现了以上两个方法,优先选择NSAttributedString的代理方法执行

 3> UIView

    可以返回自定义的视图,reusingView没有真正实现应用。

 */

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0);

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;


// component列中的row行被选中

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;


控制器.m文件

代码:

//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
//返回每列中的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    //如果是第0组 直接返回数组的长度
    if (component == 0) {
        
    return self.dataArray.count;
        
    }else{
    //如果是第1组  先确定选中的是第0组哪一行 在返回那一行的数组长度
        
      NSInteger selRow = [pickerView selectedRowInComponent:0];
        
        ProviceModel * Promodel = self.dataArray[selRow];
        
        self.selProvice = Promodel;
        
        return self.selProvice.cities.count;
    }
}
/**
<span style="font-family: Menlo;"><span style="white-space:pre">	</span><span style="color:#ff0000;">细节处理</span></span>
<span style="font-family: Menlo;"><span style="color:#ff0000;"><span style="white-space:pre">	</span>在滚动第0列的同时,滚动第1列,程序会崩溃!</span></span>
<span style="font-family: Menlo;"><span style="color:#ff0000;"><span style="white-space:pre">	</span>原因:当第0列还没有滚动完成时,第一列展示的数据还是第0列原来选中行的数据,如果新选中的行和之前</span></span>
<span style="font-family: Menlo;"><span style="color:#ff0000;"><span style="white-space:pre">	</span>     行的下标不同,会产生数组月结的问题。</span></span>
<span style="font-family: Menlo;"><span style="color:#ff0000;"><span style="white-space:pre">	</span>解决方法:</span></span>
<span style="font-family: Menlo;"><span style="color:#ff0000;"><span style="white-space:pre">		</span>将第0列选中的行数据保存起来,第1列直接访问这个保存起来的数据。</span></span>
<span style="font-family: Menlo;">*/</span>
//返回每列中每一行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
   // NSString * cityStr = model.cities[row];
    if (0 == component) {
        
        ProviceModel * model = self.dataArray[row];
        
        return model.name;
    }else{
        
        //先确定选中第0组的哪一行
//        NSInteger selIndex = [pickerView selectedRowInComponent:0];
//        
//      ProviceModel * model = self.dataArray[selIndex];
//
//        self.selProvice = model;
        
        return self.selProvice.cities[row];
    }

}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (0 == component) {
        
        [pickerView reloadComponent:1];
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值