#import "HomeTableViewCell.h"
@interface HomeViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>{
NSArray *nameArray;
NSArray *messageArray;
UITableView *tableView;
UITableView *addAddressTab;
UITextField *consignessTextFd;
UITextField *mobilePhoneTextFd;
UITextField *selectionTextFd;
UITextField *detailedDddressTextFd;
}
@property (copy, nonatomic) NSString *state;
@property (copy, nonatomic) NSString *city;
@property (copy, nonatomic) NSString *district;
@property (nonatomic,retain) NSString *selectedState;
@property (nonatomic,retain) NSString *selectedCity;
@property (nonatomic,retain) NSString *selectedArea;
@end
#import "HomeViewController.h"
@interface HomeViewController ()
{
NSArray *provinces, *cities, *areas;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//
// nameArray =[[NSArray alloc]initWithObjects:@"收货人:",@"联系手机:",@"地址选择:",@"详细地址:",nil];
//
// messageArray = [[NSArray alloc]initWithObjects:@"请输入收货人姓名",@"请输入收货人手机号码",@"请选择地区",@"请输入详细地址",nil];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.frame.size.height/3) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
// UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 44*5, self.view.bounds.size.width-100, 44)];
// [btn setTitle:@"保存地址" forState:UIControlStateNormal];
// btn.backgroundColor = [UIColor lightGrayColor];
// btn.layer.cornerRadius = 8;
// [btn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:btn];
}
-(void)save{
// NSLog(@"保存");
// UITextField *textField = (UITextField *)[tableView viewWithTag:102];
// textField.text = [NSString stringWithFormat:@"%@ %@ %@",_selectedState,_selectedCity,_selectedArea];
// [textField resignFirstResponder]; //点击返回按钮时,失去第一响应
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HomeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell=[[HomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
if (indexPath.row ==0) {
cell.contentLb.text=@"收货人";
consignessTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];
consignessTextFd.tag =100;
consignessTextFd.placeholder = @"请输入收货人姓名";
//显示文本框的光标,要去除文本框背景色
// consignessTextFd.tintColor=[UIColor whiteColor];
consignessTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;//设置清除按钮模式
[consignessTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型
consignessTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//
consignessTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮
consignessTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[cell addSubview:consignessTextFd];
}else if (indexPath.row ==1){
cell.contentLb.text=@"联系手机:";
mobilePhoneTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];
mobilePhoneTextFd.tag =101;
mobilePhoneTextFd.placeholder = @"请输入手机号码";
mobilePhoneTextFd.tintColor=[UIColor whiteColor];
mobilePhoneTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;
[mobilePhoneTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型
mobilePhoneTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//
mobilePhoneTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮
mobilePhoneTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[cell addSubview:mobilePhoneTextFd];
}else if (indexPath.row ==2){
cell.contentLb.text=@"地址选择:";
selectionTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];
selectionTextFd.tag =103;
selectionTextFd.delegate=self;
selectionTextFd.placeholder = @"请选择地址";
selectionTextFd.tintColor=[UIColor whiteColor];
selectionTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;
[selectionTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型
selectionTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//
selectionTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮
selectionTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[cell addSubview:selectionTextFd];
}else{
cell.contentLb.text=@"详细地址:";
detailedDddressTextFd = [[UITextField alloc]initWithFrame:CGRectMake(130, 8, 120, 30)];
detailedDddressTextFd.tag =104;
detailedDddressTextFd.placeholder = @"请输入详细地址";
detailedDddressTextFd.tintColor=[UIColor whiteColor];
detailedDddressTextFd.clearButtonMode = UITextFieldViewModeWhileEditing;
[detailedDddressTextFd setBorderStyle:UITextBorderStyleNone]; //外框类型
detailedDddressTextFd.font=[UIFont fontWithName:@"Times New Roman" size:15];//
detailedDddressTextFd.clearsOnBeginEditing=YES;//UITextField 的是否出现一件清除按钮
detailedDddressTextFd.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[cell addSubview:detailedDddressTextFd];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//宽和高是固定的,宽:320,高216
if (textField ==selectionTextFd) {
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-400, 0, 0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
//设置弹出视图(不弹键盘弹图片)
textField.inputView = pickerView;
//获取plist文件路径,两种方式
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"area" ofType:@"plist"];
//读取文件内容
provinces = [[NSArray alloc]initWithContentsOfFile:path];
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
//设置初始值
self.state = [[provinces objectAtIndex:0] objectForKey:@"state"]; //省
self.city = [[cities objectAtIndex:0] objectForKey:@"city"]; //市
//地区
if (areas.count > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
}
}
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
switch (component) {
case 0:
return [provinces count];
break;
case 1:
return [cities count];
break;
default:
return [areas count];
break;
}
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
switch (component) {
case 0:
return [[provinces objectAtIndex:row] objectForKey:@"state"];
break;
case 1:
return [[cities objectAtIndex:row] objectForKey:@"city"];
break;
case 2:
if ([areas count] > 0) {
return [areas objectAtIndex:row];
break;
}
default:
return @"";
break;
}
}
//选择行事件,监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];
//将指定的行滚动到中心
[pickerView selectRow:0 inComponent:1 animated:YES];
//刷新指定列中的行
[pickerView reloadComponent:1];
areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:YES];
[pickerView reloadComponent:2];
self.state = [[provinces objectAtIndex:row] objectForKey:@"state"];
self.city = [[cities objectAtIndex:0] objectForKey:@"city"];
if ([areas count] > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
break;
case 1:
areas = [[cities objectAtIndex:row] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:YES];
[pickerView reloadComponent:2];
self.city = [[cities objectAtIndex:row] objectForKey:@"city"];
if ([areas count] > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
break;
case 2:
if ([areas count] > 0) {
self.district = [areas objectAtIndex:row];
} else{
self.district = @"";
}
break;
default:
break;
}
NSInteger selectedProvinceIndex = [pickerView selectedRowInComponent:0];
_selectedState = [[provinces objectAtIndex:selectedProvinceIndex]objectForKey:@"state"];
NSInteger selectedCityIndex = [pickerView selectedRowInComponent:1];
_selectedCity = [[cities objectAtIndex:selectedCityIndex]objectForKey:@"city"];
if (areas.count > 0) {
NSInteger selectedAreaIndex = [pickerView selectedRowInComponent:2];
_selectedArea = [areas objectAtIndex:selectedAreaIndex];
} else{
_selectedArea = @"";
}
selectionTextFd.text = [NSString stringWithFormat:@"%@ %@ %@",_selectedState,_selectedCity,_selectedArea];
}
#import <UIKit/UIKit.h>
#define HEL_NEUE_REGULAR(FONT_SIZE) [UIFont fontWithName:@"HelveticaNeue" size:FONT_SIZE]
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface HomeTableViewCell : UITableViewCell
@property(retain,nonatomic)UILabel *contentLb;
@end
#import "HomeTableViewCell.h"
@implementation HomeTableViewCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
[self initUI];
}
return self;
}
-(void)initUI{
_contentLb =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 120, 21)];
_contentLb.font = HEL_NEUE_REGULAR(15);
_contentLb.textColor =UIColorFromRGB(000000);
[self.contentView addSubview:_contentLb];
}
@end