在 iOS開(kāi)發(fā)之城市選擇器一文中用兩列的UIPickerView實(shí)現(xiàn)了城市選擇器搞挣,今天用兩個(gè)UITableView來(lái)實(shí)現(xiàn)一下,首先這種聯(lián)動(dòng)在很多地方用得上壹瘟,而且方法有好幾種阅签,我這里選擇了個(gè)人喜歡的一種方式:弄兩個(gè)UITableView,讓當(dāng)前控制器管理枕面。這種方式總體思路如下:
1愿卒、添加兩個(gè)UITableView到當(dāng)前控制器中,分別設(shè)置它們的的尺寸潮秘,然后拖線到控制器中
2琼开、左邊的表格設(shè)置數(shù)據(jù)源和代理為當(dāng)前控制器,然后顯示數(shù)據(jù)枕荞,右邊的表格也設(shè)置數(shù)據(jù)源為當(dāng)前控制器柜候,然后顯示數(shù)據(jù)操作。
3买猖、監(jiān)聽(tīng)左邊表格控制器的點(diǎn)擊事件改橘,在它的點(diǎn)擊事件中刷新右邊的表格
這時(shí)候就有問(wèn)題了,一個(gè)控制器要成為2個(gè)UITableView的數(shù)據(jù)源和代理玉控,怎么辦飞主?—— 在數(shù)據(jù)源和代理方法中,進(jìn)行判斷 if (self.leftTableView== tableView) {} else{}
</br>
具體步驟:
1、添加2個(gè)UITableView碌识,設(shè)置約束碾篡,設(shè)置數(shù)據(jù)源和代理,拖線到控制器筏餐,添加plist文件(和之前文中的一樣开泽,就不貼圖了)。
添加和準(zhǔn)備工作.png
2魁瞪、在控制器中實(shí)現(xiàn)功能穆律,具體代碼如下,注釋非常詳細(xì):
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#pragma mark 定義的屬性
/**
* 左邊的表格
*/
@property (weak, nonatomic) IBOutlet UITableView* leftTableView;
/**
* 右邊的表格
*/
@property (weak, nonatomic) IBOutlet UITableView* rightTableView;
/**
* plist對(duì)應(yīng)的字典
*/
@property (nonatomic, strong) NSDictionary* cityNames;
/**
* 省份
*/
@property (nonatomic, strong) NSArray* provinces;
/**
* 城市
*/
@property (nonatomic, strong) NSArray* cities;
/**
* 當(dāng)前選擇的省份
*/
@property (nonatomic, copy) NSString* currentProvince;
/**
* 當(dāng)前選擇的城市
*/
@property (nonatomic, copy) NSString* currentCity;
@end
@implementation ViewController
#pragma mark 懶加載
/**
* 懶加載plist
*
* @return plist對(duì)應(yīng)的字典
*/
- (NSDictionary*)cityNames
{
if (_cityNames == nil) {
NSString* path = [[NSBundle mainBundle] pathForResource:@"cityData" ofType:@"plist"];
_cityNames = [NSDictionary dictionaryWithContentsOfFile:path];
}
return _cityNames;
}
/**
* 懶加載省份
*
* @return 省份對(duì)應(yīng)的數(shù)組
*/
- (NSArray*)provinces
{
if (_provinces == nil) {
//將省份保存到數(shù)組中 但是字典保存的是無(wú)序的 所以讀出來(lái)的省份也是無(wú)序的
_provinces = [self.cityNames allKeys];
}
return _provinces;
}
#pragma mark ViewController生命周期
- (void)viewDidLoad
{
[super viewDidLoad];
//一開(kāi)始的省份應(yīng)該是provinces的第一個(gè)
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;
}
//右邊的要根據(jù)選中的行來(lái)設(shè)置
else {
//通過(guò)省份去獲取對(duì)應(yīng)的城市
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
{
//點(diǎn)擊左邊加載右邊的數(shù)據(jù)
if (self.leftTableView == tableView) {
self.currentProvince = [self.provinces objectAtIndex:indexPath.row];
[self.rightTableView reloadData];
}
//點(diǎn)擊右邊顯示用戶選擇的省份和城市
else {
self.currentCity = [self.cities objectAtIndex:indexPath.row];
// 1.實(shí)例化alert:alertControllerWithTitle
NSString* msg = [NSString stringWithFormat:@"%@ -- %@", self.currentProvince, self.currentCity];
UIAlertController* alertControl = [UIAlertController alertControllerWithTitle:@"選擇城市" message:msg preferredStyle:UIAlertControllerStyleAlert];
// 2.實(shí)例化按鈕:actionWithTitle
[alertControl addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
// 點(diǎn)擊確定按鈕的時(shí)候, 會(huì)調(diào)用這個(gè)block
//[self dismissViewControllerAnimated:YES completion:nil];
}]];
// 3.顯示alertController:presentViewController
[self presentViewController:alertControl animated:YES completion:nil];
}
}
@end
3导俘、運(yùn)行結(jié)果
聯(lián)動(dòng)效果.gif