iOS开发之UITableView联动实现城市选择器

iOS开发之城市选择器一文中用两列的UIPickerView实现了城市选择器,今天用两个UITableView来实现一下,首先这种联动在很多地方用得上,而且方法有好几种,我这里选择了个人喜欢的一种方式:弄两个UITableView,让当前控制器管理。这种方式总体思路如下:

1、添加两个UITableView到当前控制器中,分别设置它们的的尺寸,然后拖线到控制器中
2、左边的表格设置数据源和代理为当前控制器,然后显示数据,右边的表格也设置数据源为当前控制器,然后显示数据操作。
3、监听左边表格控制器的点击事件,在它的点击事件中刷新右边的表格

这时候就有问题了,一个控制器要成为2个UITableView的数据源和代理,怎么办?—— 在数据源和代理方法中,进行判断 if (self.leftTableView== tableView) {} else{}

</br>

具体步骤:

1、添加2个UITableView,设置约束,设置数据源和代理,拖线到控制器,添加plist文件(和之前文中的一样,就不贴图了)。


img_e4b2a957525dbca5edfcbb0ad046ee46.png
添加和准备工作.png

2、在控制器中实现功能,具体代码如下,注释非常详细:

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

#pragma mark 定义的属性
/**
 *  左边的表格
 */
@property (weak, nonatomic) IBOutlet UITableView* leftTableView;
/**
 *  右边的表格
 */
@property (weak, nonatomic) IBOutlet UITableView* rightTableView;

/**
 *  plist对应的字典
 */
@property (nonatomic, strong) NSDictionary* cityNames;
/**
 *  省份
 */
@property (nonatomic, strong) NSArray* provinces;
/**
 *  城市
 */
@property (nonatomic, strong) NSArray* cities;

/**
 *  当前选择的省份
 */
@property (nonatomic, copy) NSString* currentProvince;

/**
 *  当前选择的城市
 */
@property (nonatomic, copy) NSString* currentCity;

@end

@implementation ViewController

#pragma mark 懒加载
/**
 *  懒加载plist
 *
 *  @return plist对应的字典
 */
- (NSDictionary*)cityNames
{
    if (_cityNames == nil) {

        NSString* path = [[NSBundle mainBundle] pathForResource:@"cityData" ofType:@"plist"];

        _cityNames = [NSDictionary dictionaryWithContentsOfFile:path];
    }

    return _cityNames;
}

/**
 *  懒加载省份
 *
 *  @return 省份对应的数组
 */
- (NSArray*)provinces
{
    if (_provinces == nil) {

        //将省份保存到数组中  但是字典保存的是无序的 所以读出来的省份也是无序的
        _provinces = [self.cityNames allKeys];
    }

    return _provinces;
}

#pragma mark ViewController生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];

    //一开始的省份应该是provinces的第一个
    self.currentProvince = self.provinces[0];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{

    //左边的返回省份即可
    if (self.leftTableView == tableView) {
        return self.provinces.count;
    }
    //右边的要根据选中的行来设置
    else {
        //通过省份去获取对应的城市
        self.cities = [self.cityNames valueForKey:self.currentProvince];
        return self.cities.count;
    }
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{

    if (self.leftTableView == tableView) {

        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"leftCell"];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"leftCell"];
        }

        //左边显示省份
        cell.textLabel.text = [self.provinces objectAtIndex:indexPath.row];

        return cell;
    }
    else {

        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"rightCell"];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rightCell"];
        }

        self.cities = [self.cityNames valueForKey:self.currentProvince];

        //右边显示城市
        cell.textLabel.text = [self.cities objectAtIndex:indexPath.row];

        return cell;
    }
}

#pragma mark UITableViewDelegate

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{

    //点击左边加载右边的数据
    if (self.leftTableView == tableView) {

        self.currentProvince = [self.provinces objectAtIndex:indexPath.row];

        [self.rightTableView reloadData];
    }
    //点击右边显示用户选择的省份和城市
    else {
        self.currentCity = [self.cities objectAtIndex:indexPath.row];

        // 1.实例化alert:alertControllerWithTitle
        NSString* msg = [NSString stringWithFormat:@"%@ -- %@", self.currentProvince, self.currentCity];
        UIAlertController* alertControl = [UIAlertController alertControllerWithTitle:@"选择城市" message:msg preferredStyle:UIAlertControllerStyleAlert];

        // 2.实例化按钮:actionWithTitle

        [alertControl addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                                    // 点击确定按钮的时候, 会调用这个block
                                    //[self dismissViewControllerAnimated:YES completion:nil];

                                }]];

        // 3.显示alertController:presentViewController
        [self presentViewController:alertControl animated:YES completion:nil];
    }
}
@end

3、运行结果

img_6a0e4f3c0e3a9d576accce4248e667c3.gif
联动效果.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * 只显示省份一级 * provinceBlock : 回调省份 */ (instancetype)provincePickerViewWithProvinceBlock:(void(^)(NSString *province))provinceBlock; /** * 显示省份和市级 * cityBlock : 回调省份和城市 */ (instancetype)cityPickerViewWithCityBlock:(void(^)(NSString *province, NSString *city))cityBlock; /** * 显示省份和市级和区域 * areaBlock : 回调省份城市和区域 */ (instancetype)areaPickerViewWithAreaBlock:(void(^)(NSString *province, NSString *city, NSString *area))areaBlock; /** * 只显示省份一级 * province : 传入了省份自动滚动到省份,没有传或者找不到默认选中第一个 * provinceBlock : 回调省份 */ (instancetype)provincePickerViewWithProvince:(NSString *)province provinceBlock:(void(^)(NSString *province))provinceBlock; /** * 显示省份和市级 * province,city : 传入了省份和城市自动滚动到选中的,没有传或者找不到默认选中第一个 * cityBlock : 回调省份和城市 */ (instancetype)cityPickerViewWithProvince:(NSString *)province city:(NSString *)city cityBlock:(void(^)(NSString *province, NSString *city))cityBlock; /** * 显示省份和市级和区域 * province,city : 传入了省份和城市和区域自动滚动到选中的,没有传或者找不到默认选中第一个 * areaBlock : 回调省份城市和区域 */ (instancetype)areaPickerViewWithProvince:(NSString *)province city:(NSString *)city area:(NSString *)area areaBlock:(void(^)(NSString *province, NSString *city, NSString *area))areaBlock;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值