zoukankan      html  css  js  c++  java
  • IOS下省市区选择器实现[基于TableView]

    最近做的IOS项目中,要用到好几处地方要用到选择器。比如说很常用的地区选择器(省市区),在android平台下已经实现了自定义一个Dialog。但是对IOS并不太熟悉,去网上下载了一些例子,大多是用PickerView,然后省市区分三列,如:



    这样的效果也不错,只是有些时候区名太长,就看不到了,而且没有按钮可以点。还有一个就是不能自己输入地址。

    衡量了一下,还是决定自己实现,最后的效果图如下:



    用户可以自己输入:



    看看源代码吧。

    先是头文件:

    //
    //  NerveAreaSelectorViewController.h
    //
    //	省市区三级选择器,可以自己输入
    //	
    //  Created by 集成显卡 on 13-5-17.
    //  Copyright (c) 2013年 集成显卡 zxingming@qq.com . All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    typedef enum {
        PROVINCE,
        CITY,
        AREA
    } NerveSelectoreType;
    
    //
    //选择器协议,在调用的ViewController中实现此协议即可
    //
    @protocol NerveAreaSelectDelegate <NSObject>
    
    @optional
    - (void)onAreaSelect:(NSString *)selectValue;
    - (void)onAreaCannel;
    @end
    
    @interface NerveAreaSelectorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
        
        NSArray *provinces, *cities, *areas;
        NSString *province, *city, *area;
        
        NSInteger selectType;//当前的选择类型,省,市,区
        
        id<NerveAreaSelectDelegate> delegate;
    }
    
    @property (weak, nonatomic) IBOutlet UITextField *areaTX;
    @property (weak, nonatomic) IBOutlet UITableView *areaTableView;
    
    
    //
    //初始化数据
    //
    -(id) initWithDelegate:(id<NerveAreaSelectDelegate>) targetDelegate;
    
    //
    //确定按钮点击事件
    //
    - (IBAction)onOkBtnClick:(id)sender;
    
    //
    //取消按钮点击事件
    //
    - (IBAction)okCannelBtnClick:(id)sender;
    
    //
    //隐藏键盘用的
    //
    - (IBAction)exitInput:(id)sender;
    
    @end


    然后是具体实现类,这里主要讲一些主要的方法。

    读取数据方法:

    -(id)initWithDelegate:(id<NerveAreaSelectDelegate>)targetDelegate{
        delegate = targetDelegate;
        
        [self readData];
        
        selectType = PROVINCE;
        self.navigationController.navigationBarHidden = NO;
        self.title = @"ssss";
        
        
        UIBarButtonItem* leftB = [[UIBarButtonItem alloc] init];
        leftB.title = @"ss";
        self.navigationItem.backBarButtonItem = leftB;
        
        return self;
    }

    显示tableCell的方法:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        int index = [indexPath row];
        //int section = [indexPath section];
        static NSString* CustomCellIdentifier = @"NerveAreaSelectorCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier];
        }
        
        switch (selectType) {
            case PROVINCE:
                cell.textLabel.text = [[provinces objectAtIndex:index]
                                       objectForKey:@"state"];
                cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
                break;
            case CITY:
                cell.textLabel.text = [[cities objectAtIndex:index]
                                       objectForKey:@"city"];
                cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
                break;
            default:
                cell.textLabel.text = [areas objectAtIndex:index];
                cell.accessoryType = UITableViewCellAccessoryNone;
                break;
        }
        
        return cell;
    }

    用户选择单元格时:

    //表格被选择
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        int selectIndex = [indexPath row];
        int cellCount = 0;
        switch (selectType) {
            case PROVINCE:
                province = [[provinces objectAtIndex:selectIndex] objectForKey:@"state"];
                cities = [[provinces objectAtIndex:selectIndex] objectForKey:@"cities"];
                
                selectType = CITY;
                cellCount = [cities count];
                break;
            case CITY:
                city = [[cities objectAtIndex:selectIndex] objectForKey:@"city"];
                areas = [[cities objectAtIndex:selectIndex] objectForKey:@"areas"];
                
                selectType = AREA;
                cellCount = [areas count];
                break;
            default:
                area = [areas objectAtIndex:selectIndex];
                break;
        }
        
        NSString* areaValue = [NSString stringWithFormat:@"%@ %@ %@",province, city,area];
        NSLog(@"select=%@", areaValue);
        
        self.areaTX.text = areaValue;
        
        [areaTableView reloadData];
        
        if(cellCount > 0){
            NSIndexPath* topIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [areaTableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
    }

    确定按钮被点击:

    - (IBAction)onOkBtnClick:(id)sender {
        NSLog(@"OK click");
        [delegate onAreaSelect:areaTX.text];
    }


    源代码下载:http://download.csdn.net/detail/ssrc0604hx/5396627




  • 相关阅读:
    VirtualBox 给虚拟机绑定IP
    【转】 wget 命令用法详解
    [转]python -m SimpleHTTPServer
    longene QQ 安装目录
    查看mininet交换机中的流表
    aircrack-ng 字典破解WPA / WPA2
    Win7 64 安装Visual Studio 2010和SQL Server 2008 R2
    Floodlight 防火墙是如何起作用的
    小米2000万买域名mi.com
    Windows JDK环境变量的配置
  • 原文地址:https://www.cnblogs.com/nerve/p/3185426.html
Copyright © 2011-2022 走看看