zoukankan      html  css  js  c++  java
  • 省市区选择器

         省市区选择器显示一般通过PickerView 和 TableView来实现,数据通过plist属性文件,sqlite,或是文本文件实现。

    我的这个是通过txt文本本件实现的。

      首先对txt文件进行解析,把解析的数据放到数组中。

      然后,用通过pickerView或tableView控件显示就ok了。

      PickerView实现的效果:

      TableView实现的效果:

    核心代码:

    View Code
    #import "ProvinceViewController.h"
    #import "CityViewController.h"
    
    @interface ProvinceViewController ()
    
    @end
    
    @implementation ProvinceViewController
    
    - (void)loadData{
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
        NSString *areaString = [NSString stringWithContentsOfFile:filePath
                                                         encoding:NSUTF8StringEncoding
                                                            error:nil];
        NSArray *areasArray = [areaString componentsSeparatedByString:@"\n"];
        _aArray = [NSMutableArray array];
        for (NSString *string in areasArray) {
            if (![string hasPrefix:@" "]) {
                NSMutableArray *cities = [NSMutableArray array];
                string = [self cutStringForSuffixBlank:string];
                NSDictionary *province = [NSDictionary dictionaryWithObjectsAndKeys:string,@"pName",cities,@"cities", nil];
                [_aArray addObject:province];
                continue;
            }
            if ([string hasPrefix:@"  "] && ![string hasPrefix:@"   "]) {
                NSDictionary *province = [_aArray lastObject];
                NSMutableArray *cities = [province objectForKey:@"cities"];
                
                NSMutableArray *areas = [NSMutableArray array];
                string = [self cutStringForPrefixBlank:string];
                string = [self cutStringForSuffixBlank:string];
                NSDictionary *city = [NSDictionary dictionaryWithObjectsAndKeys:string,@"cName",areas,@"areas", nil];
                [cities addObject:city];
                continue;
            }
            NSDictionary *city = [[[_aArray lastObject] objectForKey:@"cities"] lastObject];
            NSMutableArray *areas = [city objectForKey:@"areas"];
            string = [self cutStringForPrefixBlank:string];
            string = [self cutStringForSuffixBlank:string];
            [areas addObject:string];
        }
         _areaData = [[AreaData alloc] initWithDataArray:_aArray];
    }
    
    - (NSString *)cutStringForPrefixBlank:(NSString *)string{
        for (int i = 0; i < 5; i++) {
            if ([string hasPrefix:@" "]) {
                string = [string substringFromIndex:1];
            }
        }
        return string;
    }
    
    - (NSString *)cutStringForSuffixBlank:(NSString *)string{
        int index = 0;
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (c == ' ') {
                NSLog(@"%d",i);
                index = i;
            }
        }
        NSString *stringResult = [string substringToIndex:index];
        return stringResult;
    }
    
    - (void)createControl{
    
        CGRect rect = CGRectZero;
        rect.size.width = self.view.bounds.size.width;
        rect.size.height = self.view.bounds.size.height - 44.0;
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:rect];
        [tableView setDataSource:self];
        [tableView setDelegate:self];
        [self.view addSubview:tableView];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self loadData];
        [self createControl];
    }
    
    - (void)dealloc{
    
        [_areaData release];
        [super dealloc];
    }
    
    # pragma mark -- UITableViewDataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return [_areaData provinces];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellIdentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
        [cell.textLabel setText:[_areaData province:indexPath.row]];
        return cell;
    }
    
    # pragma mark -- UITableViewDelegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        CityViewController *city = [[[CityViewController alloc] init] autorelease];
        [city setTitle:@""];
        [city setAreaData:_areaData];
        [city setIndexForProvince:indexPath.row];
        [self.navigationController pushViewController:city animated:YES];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    Nginx 禁止IP访问
    Nginx服务优化详解
    adb不响应
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    【翻译】Android避免内存泄露(Activity的context 与Context.getApplicationContext)
    内存泄露情况
    AndroidManifest笔记
    RecyclerView设置verticalSapcing等
    Fragment回调顺序及getActivity()为NullPointerException解决方法
    git tag
  • 原文地址:https://www.cnblogs.com/hanjun/p/2909010.html
Copyright © 2011-2022 走看看