今天公司做項目用到了選擇地址,於是便寫了一個,用的純系統方法和控件,還沒有封裝,有需要的可以自行封裝一下,先上效果圖吧
刷新任意一列每一列都可以進行刷新
下面是代碼
這是聲明的內容
#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,copy)NSMutableArray * proTitleList0;
@property (nonatomic,copy)NSMutableArray * proTitleList1;
@property (nonatomic,copy)NSMutableArray * proTitleList2;
@property (nonatomic,copy)NSString * a;
@property (nonatomic,copy)NSString * b;
@property (nonatomic,copy)NSString * c;
@property (nonatomic,strong)UITextField * textField;
@property (nonatomic,strong)UIToolbar * tool;
@end
創建
- (void)viewDidLoad {
[super viewDidLoad];
[self creatData];
[self creatPickView];
}
-(void)creatData{
_proTitleList0 = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
_proTitleList1 = [[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",nil];
_proTitleList2 = [[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",nil];
}
-(void)creatPickView{
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, WIDTH, 40)];
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textField];
UIPickerView * pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 300, WIDTH, 250)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[pickerView addSubview:_tool];
_textField.inputView = pickerView;
}
-(void)breakDataproTitleList1:(NSInteger)row{
//刷新之前需要先將數組中的內容移除
// [_proTitleList1 removeAllObjects];
//重新加載數據
for (int i = 0; i < _proTitleList1.count; i ++) {
//此處是直接替換
NSString * result = [NSString stringWithFormat:@"%@%d",[_proTitleList0 objectAtIndex:row],i];
[_proTitleList1 replaceObjectAtIndex:i withObject:result];
}
}
-(void)breakDataproTitleList2:(NSInteger)row{
//刷新之前需要先將數組中的內容移除
// [_proTitleList1 removeAllObjects];
//重新加載數據
for (int i = 0; i < _proTitleList2.count; i ++) {
//此處是直接替換
NSString * result = [NSString stringWithFormat:@"%@%d",[_proTitleList1 objectAtIndex:row],i];
[_proTitleList2 replaceObjectAtIndex:i withObject:result];
}
}
// pickerView 列數
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
// pickerView 每列個數
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [_proTitleList0 count];
}else if (component == 1){
return [_proTitleList1 count];
}else{
return [_proTitleList2 count];
}
}
// 每列寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == 1) {
return WIDTH/3;
}else if(component == 0){
return WIDTH/3;
}else{
return WIDTH/3;
}
}
// 返回選中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
//在此處刷新數據proTimeList
[self breakDataproTitleList1:row];
[pickerView reloadComponent:1];
//這個地方時確保第二列變了第三列也變
[self breakDataproTitleList2:[pickerView selectedRowInComponent:1]];;
[pickerView reloadComponent:2];
} else if(component == 1){
[self breakDataproTitleList2:row];
[pickerView reloadComponent:2];
}else{
}
_a = [_proTitleList0 objectAtIndex:[pickerView selectedRowInComponent:0]];
_b = [_proTitleList1 objectAtIndex:[pickerView selectedRowInComponent:1]];
_c = [_proTitleList2 objectAtIndex:[pickerView selectedRowInComponent:2]];
_textField.text = [NSString stringWithFormat:@"第一列%@第二列%@第三列%@",_a,_b,_c];
}
//返回當前行的內容,此處是將數組中數值添加到滾動的那個顯示欄上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_proTitleList0 objectAtIndex:row];
} else if(component == 1){
return [_proTitleList1 objectAtIndex:row];
}else{
return [_proTitleList2 objectAtIndex:row];
}
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_textField resignFirstResponder];
}