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

作者: YungFan | 来源:发表于2016-11-09 10:08 被阅读1286次

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

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

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

</br>

具体步骤:

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


添加和准备工作.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、运行结果

联动效果.gif

相关文章

网友评论

  • 582c6f536166:感觉好高大上
    YungFan:@哇哈哈程 其实不难
  • c769b551ec07:说的也对!
  • c769b551ec07:之前有这样的需求,当时只是做一个列表刷新不就行了么。。联动啥的我表示不知道。。。
    c769b551ec07:@YungFan 给Tag值不是更简单么?
    c769b551ec07:@YungFan 这个理由我服 :+1:
    YungFan:@取个傻子名 这只是一个小的例子 以后有同样的需求可以按照思路来 思路是最重要的
  • 张云龙:这种文章plist文件或者demo不附上,意义何在?
    YungFan:@张云龙 意义是给有这个文件的人看的 你没有不代表别人没有
  • 幸福的李雨龙:😂简书文章越来越没品了
    YungFan:@幸福的李雨龙 ?
  • 风吹柳絮如花落:你试一下两个列表同时滚动
    YungFan:@Foundation 我没有 你自己去测试一下
    风吹柳絮如花落:@YungFan 不会越界?
    YungFan:@Foundation 可以同时滚啊
  • 叶子sir: :pray: plist文件可以发一份给我吗 lmlzzzzzzzz@163.com 万分感谢!
    叶子sir:@YungFan 多谢! :+1:
    YungFan:@叶子sir https://pan.baidu.com/s/1dETRthZ

本文标题:iOS开发之UITableView联动实现城市选择器

本文链接:https://www.haomeiwen.com/subject/axvzuttx.html