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




  • 相关阅读:
    [Leetcode]7. 整数反转
    [Leetcode]6. Z 字形变换
    [Leetcode]5. 最长回文子串
    java实现各种排序算法1
    [Leetcode]4. 寻找两个正序数组的中位数
    css设置字体单行,多行超出省略号显示
    如何获得select被选中option的value和text和......
    在vue项目中,将juery设置为全局变量
    js中遍历对象的属性和值的方法
    深入理解JS的事件绑定、事件流模型
  • 原文地址:https://www.cnblogs.com/nerve/p/3185426.html
Copyright © 2011-2022 走看看