城市选择器

效果

城市选择器

源码

https://github.com/YouXianMing/Animations ;

//
//  CustomCityPickerViewController.m
//  Animations
//
//  Created by YouXianMing on 2017/9/12.

//  Copyright © 2017年 YouXianMing. All rights reserved.

//

#import "CustomCityPickerViewController.h"
#import "PickerCityDataManager.h"
#import "FileManager.h"
#import "NSData+JSONData.h"
#import "CustomPickerView.h"
#import "PickerViewDataAdapter.h"
#import "UIView+SetRect.h"
#import "PickerProvinceView.h"
#import "PickerCityView.h"
#import "PickerAreaView.h"

@interface CustomCityPickerViewController () 

@property (nonatomic, strong) NSArray  *provinces;

@property (nonatomic, strong) CustomPickerView      *pickerView;
@property (nonatomic, strong) PickerViewDataAdapter *pickerViewDataAdapter;

@end

@implementation CustomCityPickerViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Get data.
    NSData  *data      = [NSData dataWithContentsOfFile:[FileManager bundleFileWithName:@"city.json"]];
    NSArray *provinces = [data toListProperty];
    self.provinces     = [PickerCityDataManager provinceModelsWithArray:provinces];

    // Create CustomPickerView's dataAdapter.
    self.pickerViewDataAdapter = [PickerViewDataAdapter pickerViewDataAdapterWithComponentsBlock:^(NSMutableArray *components) {

        // Province component.
        PickerViewComponent *provinceComponent = [PickerViewComponent pickerViewComponentWithRowsBlock:^(NSMutableArray *rows) {

            [self.provinces enumerateObjectsUsingBlock:^(PickerProvinceModel *provinceModel, NSUInteger idx, BOOL * _Nonnull stop) {

                [rows addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerProvinceView class] data:provinceModel]];
            }];

        } componentWidth:Width / 3.f - 5.f];

        // City component.
        PickerViewComponent *cityComponent = [PickerViewComponent pickerViewComponentWithRowsBlock:^(NSMutableArray *rows) {

            [self.provinces.firstObject.cities enumerateObjectsUsingBlock:^(PickerCityModel *cityModel, NSUInteger idx, BOOL * _Nonnull stop) {

                [rows addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerCityView class] data:cityModel]];
            }];

        } componentWidth:Width / 3.f - 5.f];

        // Area component.
        PickerViewComponent *areaComponent = [PickerViewComponent pickerViewComponentWithRowsBlock:^(NSMutableArray *rows) {

            [self.provinces.firstObject.cities.firstObject.areas enumerateObjectsUsingBlock:^(PickerAreaModel *areaModel, NSUInteger idx, BOOL * _Nonnull stop) {

                [rows addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerAreaView class] data:areaModel]];
            }];

        } componentWidth:Width / 3.f - 5.f];

        [components addObject:provinceComponent];
        [components addObject:cityComponent];
        [components addObject:areaComponent];

    } rowHeight:60.f];

    // Create CustomPickerView.
    self.pickerView = [[CustomPickerView alloc] initWithFrame:CGRectMake(0, 0, Width, 0)
                                                     delegate:self
                                         pickerViewHeightType:kCustomPickerViewHeightTypeMax
                                                  dataAdapter:self.pickerViewDataAdapter];
    self.pickerView.center = self.contentView.middlePoint;
    [self.contentView addSubview:self.pickerView];
}

#pragma mark - CustomPickerViewDelegate

- (void)customPickerView:(CustomPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (component == 0) {

        NSMutableArray *citys = [NSMutableArray array];
        [self.provinces[row].cities enumerateObjectsUsingBlock:^(PickerCityModel *cityModel, NSUInteger idx, BOOL * _Nonnull stop) {

            [citys addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerCityView class] data:cityModel]];
        }];

        NSMutableArray *areas = [NSMutableArray array];
        [self.provinces[row].cities.firstObject.areas enumerateObjectsUsingBlock:^(PickerAreaModel *areaModel, NSUInteger idx, BOOL * _Nonnull stop) {

            [areas addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerAreaView class] data:areaModel]];
        }];

        pickerView.pickerViewDataAdapter.components[1].rows = citys;
        pickerView.pickerViewDataAdapter.components[2].rows = areas;
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];

    } else if (component == 1) {

        NSInteger       provinceIndex = [self.pickerView selectedRowInComponent:0];
        NSMutableArray *areas         = [NSMutableArray array];

        // Protect crash.
        if (self.provinces[provinceIndex].cities.count  row) {

            row = self.provinces[provinceIndex].cities.count - 1;
        }

        [self.provinces[provinceIndex].cities[row].areas enumerateObjectsUsingBlock:^(PickerAreaModel *areaModel, NSUInteger idx, BOOL * _Nonnull stop) {

            [areas addObject:[PickerViewRow pickerViewRowWithViewClass:[PickerAreaView class] data:areaModel]];
        }];

        pickerView.pickerViewDataAdapter.components[2].rows = areas;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
}

- (void)customPickerView:(CustomPickerView *)pickerView didSelectedRows:(NSArray  *)rows selectedDatas:(NSArray <id> *)datas {

    NSLog(@"%@", datas);
}

@end

特点

1. 每一个row都可以像使用UITableView的cell一样可以定制,继承CustomPickerView即可

2. 数据源通过PickerViewDataAdapter来指定,每一个PickerViewComponent都有一个PickViewRow的数组,每一个 PickViewRow元素都对应一个自定义的 CustomPickerView。

Original: https://www.cnblogs.com/YouXianMing/p/7510986.html
Author: YouXianMing
Title: 城市选择器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/554398/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Spark 环境搭建(集群模式 + yarn模式)

    1 posted @2022-08-23 19:29 学而不思则罔! 阅读(6 ) 评论() 编辑 Original: https://www.cnblogs.com/bajiao…

    技术杂谈 2023年7月11日
    071
  • 如何将编写的c语言程序打包成exe可执行文件呢?

    如何将编写的c语言程序打包成exe可执行文件呢? 以前我们写程序很多是在编辑器上,让编辑起来编译运行我们的程序。如果想将其打包成exe可执行文件该如何做? 我这里推荐使用codeB…

    技术杂谈 2023年6月21日
    0138
  • SASE

    什么是 SASE? SASE一般读作”sassy”。 安全访问服务边缘简称 SASE,是一种基于云的 IT 模型,它将软件定义的网络与网络安全功能捆绑在一起…

    技术杂谈 2023年5月31日
    094
  • 网络设备配置-6、通过RIP协议配置动态路由

    一、前言 同系列前几篇:网络设备配置–1、配置交换机enable、console、telnet密码网络设备配置–2、通过交换机划分vlan网络设备配置&#8…

    技术杂谈 2023年7月11日
    0102
  • KestrelServer详解[3]: 自定义一个迷你版的KestrelServer

    和所有的服务器一样,KestrelServer最终需要解决的是网络传输的问题。在《网络连接的创建》,我们介绍了KestrelServer如何利用连接接听器的建立网络连接,并再次基础…

    技术杂谈 2023年5月30日
    081
  • 【笔试】1、强迫卖家

    小明是个强迫症卖家,有10000台设备,卖的均价要求最接近D元,输出卖出的台数N,总售价M 输入 0首先想得是暴力解答然后是二分查找,寻找卖出多少台才符合要求,但是一直找不到符合要…

    技术杂谈 2023年7月24日
    065
  • 2022.15 数字水印

    在数字化转型当下,越来越多事物在被电子数字化,我们在收获数字化便利好处的同时,也要面对数据信息更容易被泄漏、篡改、盗版等问题,而数字水印技术就是为解决上述问题的。 数字水印,是指将…

    技术杂谈 2023年5月30日
    088
  • MySQL的权限管理和Linux下的常用命令

    1.管理用户: root,具有最高权限,具有创建用户的权限,可以为其他用户授权 2.普通用户: 普通由root用户创建,权限由root分配 — mysql创建用户: create…

    技术杂谈 2023年7月24日
    071
  • ORA-01536: space quota exceeded for tablespace案例

    最近在做数据治理的过程中,回收了部分账号的权限,因为角色RESOURCE里拥有CREATE TABLE的权限,所以我想回收RESOURCE角色。例如,对于TEST账号,收回其创建表…

    技术杂谈 2023年5月30日
    091
  • 从问题域和解决域看需求评审

    01、 什么是需求评审? 需求是用户在产品使用过程中,发现产品存在无法满足的业务场景或无法解决的业务问题,进而提出的诉求。 需求评审可以区分为两个维度,一个是评审需求的问题域,即该…

    技术杂谈 2023年7月23日
    090
  • 基本电路学习-1 12v转5V 电路

    本博客是个人工作中记录,遇到问题可以互相探讨,没有遇到的问题可能没有时间去特意研究,勿扰。另外建了几个QQ技术群:2、全栈技术群:616945527,加群口令abc1232、硬件嵌…

    技术杂谈 2023年6月1日
    0164
  • BlazorVSVue

    Vue——​​两分钟概述 Vue 是一个JavaScript 框架。 在其最简单的模式中,您可以简单地将核心 Vue 脚本包含在您的应用程序中,然后开始构建您的组件。 除此之外,对…

    技术杂谈 2023年7月24日
    081
  • SpringBoot与多数据源那点事儿~

    持续原创输出,点击上方蓝字关注我 目录 前言 写这篇文章的目的 什么是多数据源? 何时用到多数据源? 整合单一的数据源 整合Mybatis 多数据源如何整合? 什么是动态数据源? …

    技术杂谈 2023年7月24日
    069
  • nacos、ribbon和feign的简明教程

    nacos简明教程 为什么需要nacos? 在微服务架构中,微服务之间经常要相互通信和调用,而且一个服务往往存在多个实例来降低负荷或保证高可用。我们假定A服务要调用B服务,最简单的…

    技术杂谈 2023年7月11日
    083
  • Redis和数据库的数据一致性问题

    在数据读多写少的情况下作为缓存来使用,恐怕是Redis使用最普遍的场景了。当使用Redis作为缓存的时候,一般流程是这样的。 如果缓存在Redis中存在,即缓存命中,则直接返回数据…

    技术杂谈 2023年7月24日
    090
  • 管正雄:基于预训练模型、智能运维的QA生成算法落地

    分享嘉宾:管正雄 阿里云 高级算法工程师 出品平台:DataFunTalk 导读:面对海量的用户问题,有限的支持人员该如何高效服务好用户?智能QA生成模型给业务带来的提效以及如何高…

    技术杂谈 2023年7月25日
    072
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球