• 省市区选择器


         省市区选择器显示一般通过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
  • 相关阅读:
    python3实现感知器,简单神经网络
    jupyter快捷键
    吴恩实验(神经网络和深度学习)第一课第三周,代码和数据集,亲测可运行
    windows下安装PyTorch0.4.0
    IDEA上传码云报错Push rejected: Push to origin/master was rejected
    IDEA常见设置
    pycharm同级目录导入问题
    解决jupyter中无自己创建的虚拟环境
    tensorflow_mmp
    python GUI
  • 原文地址:https://www.cnblogs.com/hanjun/p/2909010.html
Copyright © 2020-2023  润新知