KCSP/ProductApp/ProductApp/Other/地址弹窗/AddressListUrlAlterView.m

355 lines
11 KiB
Objective-C

//
// AddressListUrlAlterView.m
// JiaNaiWisdom
//
// Created by yx_macmini on 2021/7/20.
//
#import "AddressListUrlAlterView.h"
#import "AddressListAlterViewTableViewCell.h"
#import "BaseRequestDataController.h"
@interface AddressListUrlAlterView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UIView *viewback;
@property (nonatomic , strong) UITableView *tabview;
///
@property (nonatomic , strong) UIView *viewheader;
@property (nonatomic , strong) NSArray *arrAddressData;
///顶部可选择
@property (nonatomic , strong) UIView *viewtopitem;
///选中的数据
@property (nonatomic , strong) NSMutableArray *arrSelectItems;
///当前显示的数据
@property (nonatomic , strong) NSMutableArray *arrdata;
@end
@implementation AddressListUrlAlterView
-(id)initWithFrame:(CGRect)frame
{
if(self = [super initWithFrame:frame])
{
[self arrSelectItems];
[self nomoData];
UIView *viewback = [[UIView alloc] init];
[viewback setBackgroundColor:[UIColor whiteColor]];
[self addSubview:viewback];
[viewback mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.bottom.equalTo(self);
make.height.offset(350);
}];
_viewback = viewback;
_viewheader = [self drawHeaderView];
_tabview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
[self addSubview:_tabview];
[_tabview setBackgroundColor:[UIColor clearColor]];
[_tabview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[_tabview setDelegate:self];
[_tabview setDataSource:self];
[_tabview mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.bottom.equalTo(self);
make.height.offset(350);
}];
UIView *viewtop = [[UIView alloc] init];
[viewtop setBackgroundColor:[UIColor clearColor]];
[viewtop setUserInteractionEnabled:YES];
[self addSubview:viewtop];
[viewtop mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self);
make.bottom.equalTo(self.tabview.mas_top);
}];
UITapGestureRecognizer *tapview = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiaAction)];
[viewtop addGestureRecognizer:tapview];
}
return self;
}
-(UIView *)drawHeaderView
{
UIView *viewheader=[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80)];
[viewheader setBackgroundColor:[UIColor whiteColor]];
UILabel *lbtitle = [[UILabel alloc] init];
[lbtitle setText:@"选择"];
[lbtitle setTextColor:[UIColor blackColor]];
[lbtitle setTextAlignment:NSTextAlignmentLeft];
[lbtitle setFont:[UIFont systemFontOfSize:16]];
[viewheader addSubview:lbtitle];
[lbtitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(15);
make.top.offset(0);
make.height.offset(40);
}];
UIView *viewitem = [[UIView alloc] init];
[viewheader addSubview:viewitem];
[viewitem mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(lbtitle.mas_bottom);
make.left.offset(15);
make.height.offset(40);
}];
_viewtopitem = viewitem;
UILabel *lbqingxanz = [[UILabel alloc] init];
[lbqingxanz setText:@"请选择"];
[lbqingxanz setTextColor:[UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1]];
[lbqingxanz setTextAlignment:NSTextAlignmentLeft];
[lbqingxanz setFont:[UIFont systemFontOfSize:14]];
[viewheader addSubview:lbqingxanz];
[lbqingxanz mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(viewitem.mas_right);
make.centerY.equalTo(viewitem);
}];
UIView *viewline = [[UIView alloc] init];
[viewline setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]];
[viewheader addSubview:viewline];
[viewline mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(viewheader);
make.height.offset(1);
}];
return viewheader;
}
///绘制选择的数据按钮
-(void)drawselectBt
{
NSArray *arrtemp = _viewtopitem.subviews;
UIButton *btlast = nil;
for(UIView *view in self.viewtopitem.subviews)
{
[view removeFromSuperview];
}
for(int i = 0 ; i < _arrSelectItems.count; i++)
{
AreaprovinceListModelData *model = _arrSelectItems[i];
UIButton *btitem = [[UIButton alloc] init];
[btitem setTitle:[Tools isStringnilkong:model.name] forState:UIControlStateNormal];
[btitem setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btitem.titleLabel setFont:[UIFont systemFontOfSize:14]];
[_viewtopitem addSubview:btitem];
[btitem mas_makeConstraints:^(MASConstraintMaker *make) {
if(btlast==nil)
{
make.left.offset(0);
}
else
{
make.left.equalTo(btlast.mas_right).offset(10);
}
make.top.bottom.equalTo(self.viewtopitem);
}];
btlast = btitem;
[btitem setTag:i];
[btitem addTarget:self action:@selector(topitemAction:) forControlEvents:UIControlEventTouchUpInside];
}
if(arrtemp.count>0)
{
[self.viewtopitem mas_updateConstraints:^(MASConstraintMaker *make) {
if(btlast==nil)
{
}
else
{
make.right.equalTo(btlast.mas_right).offset(10);
}
}];
}
else
{
[self.viewtopitem mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(btlast.mas_right).offset(10);
}];
}
}
-(void)topitemAction:(UIButton *)sender
{
if(sender.tag>0)
{
[self.arrSelectItems removeObjectsInRange:NSMakeRange(sender.tag, self.arrSelectItems.count-sender.tag)];
if(sender.tag==1)
{
AreaprovinceListModelData *model = self.arrSelectItems[0];
[self getLastData:model.ID];
}
}
else
{
[self.arrSelectItems removeAllObjects];
[self.arrdata removeAllObjects];
[self.arrdata addObjectsFromArray:self.arrAddressData];
}
[self drawselectBt];
[self.tabview reloadData];
}
-(void)layoutSubviews
{
[super layoutSubviews];
[self setBackgroundColor:RGBACOLOR(0, 0, 0, 0.4)];
}
-(void)nomoData
{
self.arrdata = [NSMutableArray new];
[LoadAlterView showView];
[BaseRequestDataController requestcommonApiareaprovinceListData:self pid:@"" Callback:^(BOOL state, AreaprovinceListModel *responseObject, NSString * _Nullable describle) {
[LoadAlterView dismis];
if(state)
{
///省
self.arrAddressData = responseObject.data;
[self.arrdata addObjectsFromArray:responseObject.data];
}
else
{
[HXHud showMessage:responseObject.message afterDelayType:1];
}
[self.tabview reloadData];
}];
}
///获取下一级数据
-(void)getLastData:(NSString *)strid
{
[LoadAlterView showView];
[BaseRequestDataController requestcommonApiareaprovinceListData:self pid:strid Callback:^(BOOL state, AreaprovinceListModel *responseObject, NSString * _Nullable describle) {
[LoadAlterView dismis];
[self.arrdata removeAllObjects];
if(state)
{
[self.arrdata addObjectsFromArray:responseObject.data];
}
else
{
[HXHud showMessage:responseObject.message afterDelayType:1];
}
[self.tabview reloadData];
}];
}
-(void)dismiaAction
{
[UIView animateWithDuration:0.5 animations:^{
[self.tabview setTop:[UIScreen mainScreen].bounds.size.height];
[self.viewback setTop:[UIScreen mainScreen].bounds.size.height];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _arrdata.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strcell = @"AddressListAlterViewTableViewCell";
AddressListAlterViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strcell];
if(!cell)
{
cell = [[AddressListAlterViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strcell];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
cell.isselect = NO;
AreaprovinceListModelData *model = self.arrdata[indexPath.row];
cell.strvalue = [Tools isStringnilkong:model.name];
for(AreaprovinceListModelData *modeltemp in _arrSelectItems)
{
if([_arrdata[indexPath.row] isEqual:modeltemp])
{
cell.isselect = YES;
}
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return _viewheader;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_arrSelectItems addObject:_arrdata[indexPath.row]];
[self drawselectBt];
if(self.arrSelectItems.count==1)
{
AreaprovinceListModelData *model = self.arrdata[indexPath.row];
[self getLastData:model.ID];
}
else if (self.arrSelectItems.count==2)
{
AreaprovinceListModelData *model = self.arrdata[indexPath.row];
[self getLastData:model.ID];
}
[tableView reloadData];
////如果选择完成就返回数据
if(_arrSelectItems.count>=3)
{
if(self.delegate)
{
[self.delegate backAddressListAlterViewArr:self.arrSelectItems];
}
[self dismiaAction];
}
}
-(NSMutableArray *)arrSelectItems
{
if(_arrSelectItems==nil)
{
_arrSelectItems = [NSMutableArray new];
}
return _arrSelectItems;
}
@end