在ViewController文件中创建添加地址界面:
@property(nonatomic,strong)UILabel *selectAreaLabel;//地区显示@property(nonatomic,strong)UITextField *nameTextF;//收货人@property(nonatomic,strong)UITextField *phoneTextF;//联系方式@property(nonatomic,strong)UITextField *addressTextF;//详细地址@property(nonatomic,copy)NSString *switchStr;//选择按钮值@property(nonatomic,strong)SelectAreaView *selectView;//选择地区视图@property(nonatomic,strong)UIView *smallBgView;//选择地区下方白色区域@property(nonatomic,strong)NSMutableArray *dataArray1;//地址列表数据@property(nonatomic,strong)NSMutableArray *areaInfoArray;//返回地址相关信息
在数据请求成功后,添加半透明背景,添加可选择的地区列表:
if (success) { NSArray *itemArray = [[resultDic[@"ITEMS"] reverseObjectEnumerator] allObjects]; for (NSDictionary *dic in itemArray) { [_dataArray1 addObject:dic]; } _smallBgView = [[UIView alloc] initWithFrame:CGRectMake(0, f_Device_h, f_Device_w, f_Device_h)]; _smallBgView.backgroundColor = [UIColor darkGrayColor]; _smallBgView.alpha = 0.8; [self.view addSubview:_smallBgView]; _selectView = [[SelectAreaView alloc] initWithProvinceList:CGRectMake(0, f_Device_h, f_Device_w, f_Device_h/3*2) dataArray:_dataArray1]; [self.view addSubview:_selectView];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(downSmallBgView:) name:@"downSmallBgV" object:nil]; }
添加一个通知实现当地区选择完成落下来后,执行的方法
#pragma mark --- 接收到通知-(void)downSmallBgView:(NSNotification *)notifi{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.5]; _smallBgView.frame = CGRectMake(0, f_Device_h, f_Device_w, f_Device_h); _selectView.frame = CGRectMake(0, f_Device_h, f_Device_w, f_Device_h/3*2); [UIView commitAnimations]; NSDictionary *dic = [notifi userInfo]; _areaInfoArray = [NSMutableArray arrayWithArray:dic[@"areaArray"]]; if (_areaInfoArray.count > 0) { NSMutableString *muStr = [NSMutableString new]; for (NSDictionary *areaDic in _areaInfoArray) { NSString *nameStr = areaDic[@"adName"]; if (nameStr.length >0) { [muStr appendString:nameStr]; } } _selectAreaLabel.text = muStr; } else { _selectAreaLabel.text = @"无"; }}
自定义一个选择视图
//初始化视图方法-(id)initWithProvinceList:(CGRect)frame dataArray:(NSMutableArray *)aDataArray{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; self.itemDic = @{@"adCode":@"",@"adName":@"",@"id":@"",@"parentId":@""}; self.areaMuArray = [NSMutableArray arrayWithObjects:_itemDic,_itemDic,_itemDic, nil]; self.dataArray1 = [NSMutableArray arrayWithArray: aDataArray]; self.dataArray2 = [NSMutableArray new]; self.dataArray3 = [NSMutableArray new]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, f_Device_w, 40)]; titleLabel.text = @"选择地区"; titleLabel.textColor = [UIColor darkGrayColor]; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.font = [UIFont systemFontOfSize:15]; [self addSubview:titleLabel]; UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; closeBtn.frame = CGRectMake(f_Device_w-40, 5, 30, 30); [closeBtn setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal]; [self addSubview:closeBtn]; [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside]; //省市区按钮 for (int i = 0; i < 3; i ++) { UIButton *sBtn = [UIButton buttonWithType:UIButtonTypeCustom]; sBtn.frame = CGRectMake(20+50*i, 45, 50, 29); if (i == 0) { [sBtn setTitle:@"请选择" forState:UIControlStateNormal]; } sBtn.titleLabel.font = [UIFont systemFontOfSize:13]; sBtn.titleLabel.adjustsFontSizeToFitWidth = YES; [sBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; sBtn.tag = i+10; [sBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:sBtn]; } //分割 UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 74, f_Device_w, 1)]; lineView.backgroundColor = [UIColor lightGrayColor]; [self addSubview:lineView]; _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 75, f_Device_w, f_Device_h-f_Device_h/3-75)]; _scrollV.showsHorizontalScrollIndicator = NO; _scrollV.pagingEnabled = YES; [self addSubview:_scrollV]; for (int i = 0; i < 3; i ++) { UITableView *tableViewW = [[UITableView alloc] initWithFrame:CGRectMake(f_Device_w*i, 0, f_Device_w, f_Device_h-f_Device_h/3-75) style:UITableViewStylePlain]; tableViewW.delegate = self; tableViewW.dataSource = self; tableViewW.rowHeight = 30; tableViewW.tag = i+1; tableViewW.separatorStyle = UITableViewCellSeparatorStyleNone; [_scrollV addSubview:tableViewW]; } } return self;}
当选择地区后,执行的方法
#pragma mark --- 选中后执行方法//参数说明:1:上一个表格数组,2:点击的是第几行数据,3:标题按钮的tag值,4:滑动视图有几个f_Device_w,5:下一个表格数组-(void)showSelectViewArray1:(NSMutableArray *)aDataArray1 indexPathRow:(int)aRow buttonTag1:(int)aTag1 xPoint:(int)aXi dataArray2:(NSMutableArray *)aDataArray2{ NSDictionary *dic = [NSDictionary dictionaryWithDictionary:aDataArray1[aRow]]; UIButton *buttonN1 = (UIButton *)[self viewWithTag:aTag1]; [buttonN1 setTitle:dic[@"adName"] forState:UIControlStateNormal]; [buttonN1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_areaMuArray replaceObjectAtIndex:aXi-2 withObject:dic]; [self cityListHttpRequestIdStr:dic[@"id"] dataArray2:aDataArray2 buttonTag:aTag1 tableViewTag:aXi];}#pragma mark --- 市县区数据请求//参数说明:1:上一个表格数组中的id,2:下一个表格数组,3:标题按钮tag值,4:tableView的tag值-(void)cityListHttpRequestIdStr:(NSString *)parentIdStr dataArray2:(NSMutableArray *)aDataArray2 buttonTag:(int)btnTag tableViewTag:(int)aTag
效果图:
仿京东商城选择地区样式详细讲解源码Demo:http://download.csdn.net/detail/hbblzjy/9603813
