首页 > 其他分享 >iOS模仿京东商城中的选择地区样式

iOS模仿京东商城中的选择地区样式

时间:2022-11-28 17:06:18浏览次数:41  
标签:sBtn CGRectMake self iOS Device 京东 nonatomic NSMutableArray 商城


在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

效果图:

iOS模仿京东商城中的选择地区样式_uiview

    

iOS模仿京东商城中的选择地区样式_uiview_02


标签:sBtn,CGRectMake,self,iOS,Device,京东,nonatomic,NSMutableArray,商城
From: https://blog.51cto.com/u_15894905/5892246

相关文章

  • IOS中的数据存储方式,特点,使用情况
    数据存储的核心都是写文件,主要有四种持久化方式:属性列表(Plist),对象序列化,SQLite数据库,CoreData。存储Plist:     键值进行存储,不能存储对象。对象需要序列化编码才能......
  • iOS开发之样式多样好用的滑动视图Demo
    现在App基本上都有滑动式图的展示,尤其是新闻资讯类的用到的最多,今天就给大家展示一个多样的滑动视图样式;首先可以看到,这是几种不同的搭配模式,也可以根据style进行其他样式的......
  • iOS开发之可双向调节的Slider滑块
    滑块在很多地方都有使用,所以这里向大家展示一个自定义的可双向控制的Slider,并且可以通过代理方法获取相应的范围值,部分代码如下:属性值:/** 设置最小值 */@property(nonato......
  • iOS开发之最近的小知识点总结一
    1、NSInteger打印以及字符串的转换2、UIScrollView在iOS7中使用了Autolayout导致不能滚动3、APP包瘦身4、interactivepopgesturerecognizer使用5、UICollectionView的数......
  • iOS开发之模仿语音聊天时,显示的音量图
    因为项目中有涉及到语音的内容,所以在这里实现一下说话时语音的音量图的展示具体内容查看代码,有注释语音内容的工具类:staticidinstance;#pragmamark-单例+(instancetyp......
  • iOS开发之最近小知识总结二
    有好多都是基础知识,仅供参考,谢谢1、autolayout自动布局autoLayout需要在​​-(void)viewDidLoad​​​方法执行完后生效,所以需要在​​-(void)viewDidAppear:(BOOL)ani......
  • iOS开发之字数不一的多标签Demo
    有朋友让帮他写一个封装的字数不一的多标签视图,所以今天将代码展示一下,供大家学习代码中封装了两种方法,分别是:1.传递数组,数组中是NSString类型的方法;2.传递数组,数组中是NSDi......
  • iOS开发之自己封装的提示框(警告框)样式BHAlertView
    最近需要使用到提示框(警告框)进行信息的展示和提醒,所以进行了一个类的封装,想用Swift调用此OC文件,但是发现有些困难,所以暂时先把OC代码进行展示,随后再好好研究一下在Swift中的......
  • axios的封装
    新建network文件夹及request.js  importaxiosfrom'axios'exportfunctionrequest(config){//创建axios实例constinstance=axios.create({......
  • axios请求
    安装npminstallaxios--save axios基本使用axios({url:'http://123.207.32.32:8000/home/multidata',method:'get'//不写默认get方式}).then(res......