UITableView的省市主从表
一、主从表的创建
- 选择 TableView Controller。
- 拖动到storyboard中。
- 设置cell的标示符(Identifier),这个必须要注意,代码中使用的标示符,和这个一样。
- 创建我们需要的控制器,继承自 UITableViewController
- 给我们在 3 中创建的UITableViewController 界面,设置它的控制器
- 如果需要指定那个界面为第一个界面,请选中 属性 设置中的 “Is Initial VIew Controller”
- 在对应的控制器内完成操作。
二、push界面(NavigatioonController)
我们想通过push 这种来推出新的控制器,那么我们需要使用 NavigationController
1. 选择 Navigation Controller。
2. 拖动到storyboard中。
3. 指定为第一个界面,选中 属性 设置中的 “Is Initial VIew Controller”
4. 连线到需要的界面中
5. 加载和设置其他界面
注意:我们在使员工push方式的时候, 第二个界面要按”返回“回到 第一个界面,那么这个“返回”要设置到第一个的返回按钮中。
如:从“省份列表”—–>”城市列表”
三、代码
1. MainUITableViewController.m
//
// MainUITableViewController.m
// 03_UIView20_UITableView5_主从表
//
// Created by 杞文明 on 16/1/3.
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "MainUITableViewController.h"
#import "CityUITableViewController.h"
@interface MainUITableViewController(){
NSMutableArray * _provinceListM;
NSMutableDictionary * _citiesDicM;
}
@end
@implementation MainUITableViewController
/**加载*/
- (void)viewDidLoad{
[super viewDidLoad];
//1.加载省数据
_provinceListM = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"provinces.plist" ofType:nil]];
//2.加载市数据
_citiesDicM = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil]];
}
/**行数*/
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _provinceListM.count;
}
/**每个单元格*/
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
static NSString* identifier = @"myCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell==nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell.textLabel setText:_provinceListM[indexPath.row]];
return cell;
}
/**目标从之气调用之前*/
-(void)prepareForSegue:(nonnull UIStoryboardSegue *)segue sender:(nullable id)sender{
//1.获取选中的省数据
NSString * provinceStr = _provinceListM[self.tableView.indexPathForSelectedRow.row];
//2.获取选中的省对应的市数据
NSArray* citiesList = _citiesDicM[provinceStr];
//3.获取目标控制器
CityUITableViewController* desCon = segue.destinationViewController;
//4.把市数据传递给目标控制器
[desCon setCitiesList:citiesList];
[desCon setProvinceName:provinceStr];
}
//- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
// NSLog(@"选中的是----"+)
// [self dismissViewControllerAnimated:YES completion:nil];
//}
@end
2. CityUITableViewController.m
//
// CityUITableViewController.m
// 03_UIView20_UITableView5_主从表
//
// Created by 杞文明 on 16/1/3.
// Copyright © 2016年 杞文明. All rights reserved.
//
#import "CityUITableViewController.h"
@implementation CityUITableViewController
/**总的行数*/
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _citiesList.count;
}
/**返回每个单元格*/
-(UITableViewCell*)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
static NSString * identifier = @"myCityCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
[cell.textLabel setText:_citiesList[indexPath.row]];
return cell;
}
/**选中*/
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
NSString * cityName = _citiesList[indexPath.row];
NSLog(@"%@--%@",_provinceName,cityName);
[self performSelector:@selector(dismissMy) withObject:self afterDelay:1];
}
/**关闭*/
-(void)dismissMy{
// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
@end