效果圖:數組
左邊圖片的代碼:app
// // SecViewController.m // UI__多個TableView練習 // // Created by dllo on 16/3/17. // Copyright © 2016年 dllo. All rights reserved. // #import "SecViewController.h" @interface SecViewController () < UITableViewDataSource, UITableViewDelegate > @property (nonatomic, retain) UITableView *tableView; @property (nonatomic, retain) NSMutableArray *proArr; @property (nonatomic, retain) NSMutableArray *cityArr; @end @implementation SecViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self createData]; [self createView]; } // 加載界面函數 - (void)createView { self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [_tableView release]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; } // 返回區(省)的個數 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.proArr.count; } // 返回各個section的省名 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.proArr[section][@"proName"]; } // 返回每一個section的row的數量 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSDictionary *proDic = self.proArr[section]; NSArray *cityArr = proDic[@"cityArr"]; return cityArr.count; } // 將市名顯示在row當中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]; NSDictionary *proDic = self.proArr[indexPath.section];//indepath.section返回indexpath的section NSArray *cityArr = proDic[@"cityArr"]; cell.textLabel.text = cityArr[indexPath.row][@"cityName"]; return cell; } // 一個返回UIView的對section條進行增長控件的方法 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; view.backgroundColor = [UIColor cyanColor]; //寫"更多"的按鈕 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(360, 0, 40, 40)]; //寫有省名的標籤 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 40)]; [label setText:self.proArr[section][@"proName"]]; // label.backgroundColor = [UIColor whiteColor]; [view addSubview:label]; [label release]; //button.backgroundColor = [UIColor whiteColor]; [view addSubview:button]; [button setTitle:@"更多" forState:0]; [button release]; return view; }// 加載數據 - (void)createData { // NSString *path = [[NSBundle mainBundle] pathForResource:@"area副" ofType:@"txt"]; // 根據路徑產生相應的字符串 NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // 對字符串逐行進行切割 NSArray *strArr = [str componentsSeparatedByString:@"\n"]; self.proArr = [NSMutableArray array]; // 將數據解析成JSON數據 for (NSString *temp in strArr) { if (![temp hasPrefix:@" "]) { NSMutableDictionary *proDic = [NSMutableDictionary dictionary]; NSMutableArray *cityArr = [NSMutableArray array]; [proDic setObject:temp forKey:@"proName"]; [proDic setObject:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; }else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]){ NSMutableDictionary *cityDic = [NSMutableDictionary dictionary]; [cityDic setObject:temp forKey:@"cityName"]; NSMutableArray *zoneArr = [NSMutableArray array]; [cityDic setObject:zoneArr forKey:@"zoneArr"]; NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; [cityArr addObject:cityDic]; }else{ NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; NSMutableDictionary *cityDic = [cityArr lastObject]; NSMutableArray *zoneArr = cityDic[@"zoneArr"]; [zoneArr addObject:temp]; } } NSLog(@"%@", self.proArr); } // 顯示側邊省名第一個漢字的方法(索引) - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSMutableArray *array = [NSMutableArray array]; //將字符串進行截取放到數組中 for (int i = 0; i < 31; i++) { NSString *string = self.proArr[i][@"proName"]; [array addObject:[string substringToIndex:1]]; } //第二種方法 : 對數組進行遍歷而後放到數組中 // for (NSDictionary *dict in self.proArr) { // [array addObject:[dict[@"proName"] substringToIndex:1]]; // } return array; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
右邊圖片的代碼:函數
// // RootViewController.m // UI__多個TableView練習 // // Created by dllo on 16/3/17. // Copyright © 2016年 dllo. All rights reserved. // #import "RootViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface RootViewController () < UITableViewDataSource, UITableViewDelegate > @property (nonatomic, retain) UITableView *proTableView; @property (nonatomic, retain) UITableView *cityTableView; @property (nonatomic, retain) UITableView *zoneTableView; @property (nonatomic, retain) NSMutableArray *proArr; @property (nonatomic, retain) NSMutableArray *zoneArr; @property (nonatomic, retain) NSMutableArray *cityArr; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //下面是三個tableView的初始化和代理設置 self.proTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT)]; self.proTableView.backgroundColor = [UIColor blueColor]; [self.view addSubview:self.proTableView]; [_proTableView release]; self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3, HEIGHT - 64)]; self.cityTableView.backgroundColor = [UIColor cyanColor]; [self.view addSubview:self.cityTableView ]; [_cityTableView release]; self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64)]; self.zoneTableView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.zoneTableView]; [_zoneTableView release]; [self.proTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; [self.cityTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; [self.zoneTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])]; self.proTableView.delegate = self; self.proTableView.dataSource = self; self.cityTableView.dataSource = self; self.cityTableView.delegate = self; self.zoneTableView.delegate = self; self.zoneTableView.dataSource = self; NSString *path = [[NSBundle mainBundle] pathForResource:@"area副" ofType:@"txt"]; // 根據路徑產生相應的字符串 NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // 對字符串逐行進行切割 NSArray *strArr = [str componentsSeparatedByString:@"\n"]; self.proArr = [NSMutableArray array]; //對數據進行解析成Json數據 for (NSString *temp in strArr) { if (![temp hasPrefix:@" "]) { NSMutableDictionary *proDic = [NSMutableDictionary dictionary]; NSMutableArray *cityArr = [NSMutableArray array]; [proDic setObject:temp forKey:@"proName"]; [proDic setObject:cityArr forKey:@"cityArr"]; [self.proArr addObject:proDic]; }else if ([temp hasPrefix:@" "] && ![temp hasPrefix:@" "]){ NSMutableDictionary *cityDic = [NSMutableDictionary dictionary]; [cityDic setObject:temp forKey:@"cityName"]; NSMutableArray *zoneArr = [NSMutableArray array]; [cityDic setObject:zoneArr forKey:@"zoneArr"]; NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; [cityArr addObject:cityDic]; }else{ NSMutableDictionary *proDic = [self.proArr lastObject]; NSMutableArray *cityArr = proDic[@"cityArr"]; NSMutableDictionary *cityDic = [cityArr lastObject]; NSMutableArray *zoneArr = cityDic[@"zoneArr"]; [zoneArr addObject:temp]; } } NSLog(@"%@", self.proArr); } - (void)dealloc { [_zoneTableView release]; [_cityTableView release]; [_proArr release]; [_proTableView release]; [super dealloc]; } // 返回第section個區的row的個數 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.proTableView) { return self.proArr.count; } else if (tableView == self.cityTableView) { return self.cityArr.count; } else { return self.zoneArr.count; } } // TableView的cell的加載方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.proTableView) {//當tableView是省時 UITableViewCell *cell = [self.proTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]; cell.textLabel.text = self.proArr[indexPath.row][@"proName"]; return cell; } else if (tableView == self.cityTableView) {//當tableView是市時 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]; cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"]; return cell; } else {//當tableView是區名時 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath]; cell.textLabel.text = self.zoneArr[indexPath.row]; return cell; } } //TableView的點擊方法 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.proTableView) {//當點擊省名時 self.cityArr = self.proArr[indexPath.row][@"cityArr"]; [self.cityTableView reloadData]; } else if (tableView == self.cityTableView) {//點擊市名時 self.zoneArr = self.cityArr[indexPath.row][@"zoneArr"]; [self.zoneTableView reloadData]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end