UI總結(jié)-多個tableView

UI總結(jié)-多個tableView

如何在一個頁面鋪多個tableView,下面是對省市區(qū)(數(shù)據(jù)的格式是.TXT)的羅列,分別在三個tableView上顯示,點擊省顯示對應省的市,點擊市顯示對應市的區(qū),來看代碼吧:
#import "ViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,retain)NSMutableArray *cityArr;
@property(nonatomic,retain)NSMutableArray *zoneArr;
@property(nonatomic,retain)UITableView *proTableView;
@property(nonatomic,retain)UITableView *cityTableView;
@property(nonatomic,retain)UITableView *zoneTableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    [self creatData];
}
-(void)creatView{
    //automaticallyAdjustsScrollViewInsets根據(jù)按所在界面的status bar,navigationbar,與tabbar的高度,自動調(diào)整scrollview的 inset,設置為no,不讓viewController調(diào)整,我們自己修改布局即可
    self.automaticallyAdjustsScrollViewInsets = YES;
    //省對應的tableView
    self.proTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH / 3, HEIGHT) style:UITableViewStylePlain];
    self.proTableView.dataSource = self;
    self.proTableView.delegate = self;
    [self.view addSubview:self.proTableView];
    [self.proTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    //城市對應的tableView
    self.cityTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3, 64, WIDTH / 3 , HEIGHT - 64) style:UITableViewStylePlain];
    self.cityTableView.dataSource = self;
    self.cityTableView.delegate = self;
    [self.view addSubview:self.cityTableView];
    [self.cityTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    //區(qū)對應的tableView
    self.zoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH / 3 * 2, 64, WIDTH / 3, HEIGHT - 64) style:UITableViewStylePlain];
    self.zoneTableView.dataSource = self;
    self.zoneTableView.delegate = self;
    [self.view addSubview:self.zoneTableView];
    [self.zoneTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
}
-(void)creatData{
    //接受數(shù)據(jù)
    //找文件的路徑
    NSString *path = @"/Users/dllo/Desktop/UI總結(jié)/多個tableView的練習/多個tableView的練習/area.txt";
    //將.txt文件的內(nèi)容全部賦值給字符串str
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    //把str字符串按換行截成字符串賦值給數(shù)組
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.proArr = [NSMutableArray array];
    //對數(shù)組進行遍歷
    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];
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //點擊方法:點擊省的tableView,對應省的市的tableView加載
    //點擊市的tableView,對應市的區(qū)的tableView加載
    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];
    }
}
-(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;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.proTableView) {
        UITableViewCell *cell = [self.proTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.text = self.proArr[indexPath.row][@"proName"];
        return cell;
    }else if (tableView == self.cityTableView){
        UITableViewCell *cell = [self.cityTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        cell.textLabel.text = self.cityArr[indexPath.row][@"cityName"];
        return cell;
    }else {
        UITableViewCell *cell = [self.zoneTableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
        cell.textLabel.text = self.zoneArr[indexPath.row];
        return cell;
    }
    
}
運行結(jié)果如下圖:
1.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容