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

lianghua 8年前發布 | 11K 次閱讀 iOS開發 移動開發 UITableView

在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];
}

- (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 loadData];
        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 loadData];

        //右邊顯示城市
        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 loadData];

        [self.rightTableView reloadData];
    }
    //點擊右邊顯示用戶選擇的省份和城市
    else {
        self.currentCity = [self.cities objectAtIndex:indexPath.row];

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

        // 2.添加按鈕:actionWithTitle
        [alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
                                    // 點擊確定按鈕的時候, 會調用這個block
                                }]];

        // 3.顯示alertController:presentViewController
        [self presentViewController:alert animated:YES completion:nil];
    }
}

#pragma mark Other

/**
 *  根據左邊選中的省份去加載右邊的城市
 */
- (void)loadData
{

    //獲左邊取選中的行
    NSInteger selRow = self.leftTableView.indexPathForSelectedRow.row;
    //選中行的省份
    self.currentProvince = [self.provinces objectAtIndex:selRow];
    //通過省份去獲取對應的城市
    self.cities = [self.cityNames valueForKey:self.currentProvince];
}

@end

3、運行結果

聯動效果.gif

 

來自:http://www.jianshu.com/p/483ba26a4c10

 

 本文由用戶 lianghua 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!