iOS開發之UITableView聯動實現城市選擇器

yungfan發表於2016-11-09

iOS開發之城市選擇器一文中用兩列的UIPickerView實現了城市選擇器,今天用兩個UITableView來實現一下,首先這種聯動在很多地方用得上,而且方法有好幾種,我這裡選擇了個人喜歡的一種方式:弄兩個UITableView,讓當前控制器管理。這種方式總體思路如下:

1、新增兩個UITableView到當前控制器中,分別設定它們的的尺寸,然後拖線到控制器中
2、左邊的表格設定資料來源和代理為當前控制器,然後顯示資料,右邊的表格也設定資料來源為當前控制器,然後顯示資料操作。
3、監聽左邊表格控制器的點選事件,在它的點選事件中重新整理右邊的表格

這時候就有問題了,一個控制器要成為2個UITableView的資料來源和代理,怎麼辦?——  在資料來源和代理方法中,進行判斷  if (self.leftTableView== tableView) {} else{}

具體步驟:

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

相關文章