基于MMPopupView和STDPickerView,数据源为
2018-12-11 本文已影响0人
AbyssLich
MMPopupView
GitHub: https://github.com/adad184/MMPopupView
STDPickerView
GitHub: https://github.com/XuQibin/STDPickerView
暂未写关于默认选择的内容,代码很糙
MMSTDAddressView.h
#import "MMPopupView.h"
NS_ASSUME_NONNULL_BEGIN
@interface MMSTDAddressView : MMPopupView
@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UIButton *btnConfirm;
@property (nonatomic, strong) NSString *pCode, *cCode, *adCode, *pName, *cName, *adName;
@end
NS_ASSUME_NONNULL_END
MMSTDAddressView.m
#import "MMSTDAddressView.h"
#import "STDPickerView.h"
@interface MMSTDAddressView ()<STDPickerViewDelegate, STDPickerViewDataSource>
{
NSMutableArray *pNameArray, *pCodeArray, *cNameArray, *cCodeArray, *adNameArray, *adCodeArray;
}
@property (nonatomic, strong) STDPickerView *pickerView;;
@end
@implementation MMSTDAddressView
- (instancetype)init {
self = [super init];
if (self) {
self.type = MMPopupTypeSheet;
pCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
pNameArray = [[NSMutableArray alloc] initWithCapacity:0];
cCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
cNameArray = [[NSMutableArray alloc] initWithCapacity:0];
adCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
adNameArray = [[NSMutableArray alloc] initWithCapacity:0];
self.backgroundColor = [UIColor whiteColor];
[MMPopupWindow sharedWindow].touchWildToHide = YES;
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
make.height.mas_equalTo(216+50);
}];
self.btnCancel = [UIButton mm_buttonWithTarget:self action:@selector(cancelClicked)];
[self addSubview:self.btnCancel];
[self.btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(80, 50));
make.left.top.equalTo(self);
}];
[self.btnCancel setTitle:@"取消" forState:UIControlStateNormal];
[self.btnCancel setTitleColor:MMHexColor(0xE76153FF) forState:UIControlStateNormal];
self.btnConfirm = [UIButton mm_buttonWithTarget:self action:@selector(confirmClicked)];
[self addSubview:self.btnConfirm];
[self.btnConfirm mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(80, 50));
make.right.top.equalTo(self);
}];
[self.btnConfirm setTitle:@"确定" forState:UIControlStateNormal];
[self.btnConfirm setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
self.pickerView = [[STDPickerView alloc] init];
_pickerView.dataSource = self;
_pickerView.delegate = self;
_pickerView.forceItemTypeText = YES;
//是否显示垂直分割线
_pickerView.showVerticalDivisionLine = YES;
//设置pickerView四周的间距
_pickerView.edgeInsets = UIEdgeInsetsMake(0, 20, 0, 20);
//设置component之间的间距
_pickerView.spacingOfComponents = 30;
//仅在文本模式下有效
_pickerView.textColor = UIColorFromHex(0x999999);
_pickerView.selectedTextColor = UIColorFromHex(0x00bf51);
_pickerView.font = [UIFont systemFontOfSize:16];
[self addSubview:self.pickerView];
[self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self).insets(UIEdgeInsetsMake(50, 0, 0, 0));
}];
}
return self;
}
#pragma mark - STDPickerViewDataSource
//返回component数目
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
//返回row数目
- (NSInteger)pickerView:(STDPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0) {
if (pCodeArray.count == 0){
return 1;
} else {
return pCodeArray.count;
}
} else if (component == 1) {
if (cCodeArray.count == 0){
return 1;
} else {
return cCodeArray.count;
}
} else {
if (adCodeArray.count == 0){
return 1;
} else {
return adCodeArray.count;
}
}
}
- (NSString *)pickerView:(STDPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0) {
if (pNameArray.count == 0) {
[self getListRegion:@"" level:0 success:^(NSArray * nameArray, NSArray * codeArray) {
self->pNameArray = [nameArray mutableCopy];self.pName = self->pNameArray[0];
self->pCodeArray = [codeArray mutableCopy];self.pCode = self->pCodeArray[0];
[self->_pickerView reloadComponent:0];
[self getListRegion:self->pCodeArray[0] level:1 success:^(NSArray * nameArray, NSArray * codeArray) {
self->cNameArray = [nameArray mutableCopy];self.cName = self->cNameArray[0];
self->cCodeArray = [codeArray mutableCopy];self.cCode = self->cCodeArray[0];
[self->_pickerView reloadComponent:1];
[self getListRegion:self->cCodeArray[0] level:2 success:^(NSArray * nameArray, NSArray * codeArray) {
self->adNameArray = [nameArray mutableCopy];self.adName = self->adNameArray[0];
self->adCodeArray = [codeArray mutableCopy];self.adCode = self->adCodeArray[0];
[self->_pickerView reloadComponent:2];
}];
}];
}];
return @"";
} else {
return pNameArray[row];
}
} else if (component == 1) {
if (cNameArray.count == 0) {
if (pCodeArray.count != 0) {
[self getListRegion:self->pCodeArray[0] level:1 success:^(NSArray * nameArray, NSArray * codeArray) {
self->cNameArray = [nameArray mutableCopy];self.cName = self->cNameArray[0];
self->cCodeArray = [codeArray mutableCopy];self.cCode = self->cCodeArray[0];
[self->_pickerView reloadComponent:1];
[self getListRegion:self->cCodeArray[0] level:2 success:^(NSArray * nameArray, NSArray * codeArray) {
self->adNameArray = [nameArray mutableCopy];self.adName = self->adNameArray[0];
self->adCodeArray = [codeArray mutableCopy];self.adCode = self->adCodeArray[0];
[self->_pickerView reloadComponent:2];
}];
}];
}
return @"";
} else {
return cNameArray[row];
}
} else {
if (adNameArray.count == 0) {
if (cCodeArray.count != 0) {
[self getListRegion:self->cCodeArray[0] level:2 success:^(NSArray * nameArray, NSArray * codeArray) {
self->adNameArray = [nameArray mutableCopy];self.adName = self->adNameArray[0];
self->adCodeArray = [codeArray mutableCopy];self.adCode = self->adCodeArray[0];
[self->_pickerView reloadComponent:2];
}];
}
return @"";
} else {
return adNameArray[row];
}
}
}
#pragma mark - STDPickerViewDelegate
//返回条目高度
- (CGFloat)pickerView:(STDPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30;
}
//选中了某个条目
- (void)pickerView:(STDPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"pickerView - didSelectRow %zd inComponent %zd", row, component);
if (component == 0) {
if (pCodeArray.count != 0) {
[self getListRegion:pCodeArray[row] level:1 success:^(NSArray * nameArray, NSArray * codeArray) {
// [self->cNameArray removeAllObjects];
// [self->cCodeArray removeAllObjects];
self->cNameArray = [nameArray mutableCopy];self.cName = self->cNameArray[0];
self->cCodeArray = [codeArray mutableCopy];self.cCode = self->cCodeArray[0];
[self->_pickerView reloadComponent:1];
[self getListRegion:self->cCodeArray[0] level:2 success:^(NSArray * nameArray, NSArray * codeArray) {
// [self->adNameArray removeAllObjects];
// [self->adCodeArray removeAllObjects];
self->adNameArray = [nameArray mutableCopy];self.adName = self->adNameArray[0];
self->adCodeArray = [codeArray mutableCopy];self.adCode = self->adCodeArray[0];
[self->_pickerView reloadComponent:2];
}];
}];
_pName = pNameArray[row];
_pCode = pCodeArray[row];
}
}else if (component == 1){
if (cCodeArray.count != 0) {
[self getListRegion:cCodeArray[row] level:2 success:^(NSArray * nameArray, NSArray * codeArray) {
// [self->adNameArray removeAllObjects];
// [self->adCodeArray removeAllObjects];
self->adNameArray = [nameArray mutableCopy];self.adName = self->adNameArray[0];
self->adCodeArray = [codeArray mutableCopy];self.adCode = self->adCodeArray[0];
[self->_pickerView reloadComponent:2];
}];
_cName = cNameArray[row];
_cCode = cCodeArray[row];
}
} else {
if (adCodeArray.count != 0) {
_adName = adNameArray[row];
_adCode = adCodeArray[row];
}
}
}
#pragma btnClicked
- (void) cancelClicked {
[self hide];
}
- (void) confirmClicked {
[self hide];
}
#pragma networking
- (void) getListRegion:(NSString *)code
level:(NSInteger)level
success:(void (^) (NSArray * _Nonnull, NSArray * _Nonnull))success {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//适配HTTPS
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[securityPolicy setValidatesDomainName:NO];
securityPolicy.allowInvalidCertificates = YES; //还是必须设成YES
manager.securityPolicy = securityPolicy;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
((AFJSONResponseSerializer *)manager.responseSerializer).removesKeysWithNullValues = YES;
NSString *URL = [BASE_URL stringByAppendingString:[NSString stringWithFormat:@"/api/listRegion"]];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:0];
[parameters setObject:[NSString stringWithFormat:@"%ld", level] forKey:@"level"];
switch (level) {
case 1://查市
[parameters setObject:[code substringToIndex:2] forKey:@"code"];
break;
case 2://查区
[parameters setObject:[code substringToIndex:4] forKey:@"code"];
break;
default:
break;
}
[manager GET:URL parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if(responseObject){
NSLog(@"%@",responseObject);
if([[responseObject objectForKey:@"status"]intValue] == 0){
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithCapacity:0];
NSMutableArray *codeArray = [[NSMutableArray alloc] initWithCapacity:0];
for(NSDictionary *dic in [responseObject objectForKey:@"data"]) {
[nameArray addObject:dic[@"name"]];
[codeArray addObject:dic[@"code"]];
}
success(nameArray, codeArray);
}else{
}
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
}
@end