• 多个TableView的练习


    效果图:

    左边图片的代码:

    //
    //  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:@"
    "];
        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:@"
    "];
        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
  • 相关阅读:
    第06课:GDB 常用命令详解(下)
    第05课:GDB常用命令详解(中)
    第04课:GDB常用命令详解(上)
    第03课:GDB常用的调试命令概览
    第02课:启动GDB调试
    第01课:调试信息与调试原理
    数据库(二)
    数据库笔记(一)
    acedSSGet 翻译
    ObjectARX动态添加AutoCAD传统下拉菜单入门篇(一)
  • 原文地址:https://www.cnblogs.com/mafeng/p/5289203.html
Copyright © 2020-2023  润新知