一個頁面裏面有三個tableView,進行互相的聯動,點擊省顯示對應的市,點擊市顯示對應的區
1.創建三個數組的屬性
代碼:
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic ,retain)NSMutableArray *cityArr;
@property(nonatomic ,retain)NSMutableArray *zoneArr;
三個tableView的屬性
@property(nonatomic,retain)UITableView *proTableView;
@property(nonatomic ,retain)UITableView *cityTableView;
@property(nonatomic ,retain)UITableView *zoneTableView;
注意:如果在初始化方法裏使用self.view,此時還沒有創建self.view,系統會自動調用loadView,創建一個self.view,從而改變VC的運行流程,所以我們只在初始化方法裏初始化容器的數據部分,不創建視圖
2.初始化並解析字典:
代碼:
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self creatData];
}
return self;
}
-(void)creatData{
NSString *path=@"/Users/dllo/Desktop/上課內容/ui/UI09_多種tableView/UI09_多種tableView/area.txt";
NSString *str =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *strArr =[str componentsSeparatedByString:@"\n"];
self.proArr =[NSMutableArray array];
for (NSString *temp in strArr) {
if (![temp hasPrefix:@" " ]) {
NSMutableDictionary *proDic =[NSMutableDictionary dictionary];
[proDic setValue:temp forKey:@"proName"];
NSMutableArray *cityArr=[NSMutableArray array];
[proDic setValue:cityArr forKey:@"cityArr"];
[self.proArr addObject:proDic];
}else if([temp hasPrefix:@" "]&&![temp hasPrefix:@" "] ){
NSMutableDictionary *cityDic=[NSMutableDictionary dictionary];
[cityDic setValue:temp forKey:@"cityName"];
NSMutableArray *zoneArr=[NSMutableArray array];
[cityDic setValue:zoneArr forKey:@"zoneArr"];
NSMutableArray *cityArr=[[self.proArr lastObject] objectForKey:@"cityArr"];
[cityArr addObject:cityDic];
}else{
NSMutableArray *zoneArr =[[[[self.proArr lastObject] objectForKey:@"cityArr"] lastObject] objectForKey:@"zoneArr"];
[zoneArr addObject:temp];
}
}
}
3.創建tableView
不要忘記代理人的設置和簽訂協議
設置背景顏色
self.view.backgroundColor=[UIColor yellowColor];
取消透明度
self.navigationController.navigationBar.translucent=NO;
省的tableView
代碼:
self.proTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:self.proTableView];
self.proTableView.delegate =self;
self.proTableView.dataSource=self;
[_proTableView release];
創建市的tableView
self.cityTableView =[[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:self.cityTableView];
[_cityTableView release];
self.cityTableView.delegate =self;
self.cityTableView.dataSource=self;
創建區的tableView
self.zoneTableView =[[UITableView alloc] initWithFrame:CGRectMake(2*self.view.frame.size.width/3, 0, self.view.frame.size.width/3, self.view.frame.size.height-64) style:UITableViewStylePlain];
[self.view addSubview:self.zoneTableView];
[_zoneTableView release];
self.zoneTableView.delegate =self;
self.zoneTableView.dataSource =self;
4.必須出發的協議(1)返回行數
代碼:
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == self.proTableView) {
NSLog(@"省,proArr.count = %ld",self.proArr.count);
return self.proArr.count;
}
else if (tableView == self.cityTableView){
//
NSLog(@"市,cityArr.count = %ld",self.cityArr.count);
return self.cityArr.count;
}else{
NSLog(@"區,zoneArr.count = %ld",self.zoneArr.count);
return self.zoneArr.count;
}
}
5.必須觸發的協議(2)創建cell
代碼:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == self.proTableView) {
NSLog(@"sheng");
static NSString *proReuse =@"proReuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:proReuse];
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:proReuse] autorelease];
}
cell.textLabel.text=self.proArr[indexPath.row][@"proName"];
return cell;
}else if(tableView == self.cityTableView){
NSLog(@"shi");
static NSString *cityReuse =@"cityReuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cityReuse];
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cityReuse] autorelease];
}
cell.textLabel.text=self.cityArr[indexPath.row][@"cityName"];
return cell;
}else{
NSLog(@"qu");
static NSString *zoneReuse =@"zoneReuse";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:zoneReuse];
if (!cell) {
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:zoneReuse] autorelease];
}
cell.textLabel.text=self.zoneArr[indexPath.row];
return cell;
}
}
6,點擊觸發協議
代碼:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// self.clickIndexPath =indexPath;
// [self.cityTableView reloadData];
// 當前是哪一個tableView被點擊
if(tableView ==self.proTableView){
// 先找到是哪個省
// NSMutableDictionary *proDic =self.proArr[indexPath.row];
self.cityArr=self.proArr[indexPath.row][@"cityArr"];
//對市數組進行reloaddate
[self.cityTableView reloadData];
// self.zoneArr =nil;
// [self.zoneTableView reloadData];
self.zoneArr=nil;
[self.zoneTableView reloadData];
}else if (tableView == self.cityTableView){
// 找市字典,再找區數組
self.zoneArr =self.cityArr[indexPath.row][@"zoneArr"];
// 對區數組進行刷新
[self.zoneTableView reloadData];
}
}