效果图:

左边图片的代码:

//
// 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(, , self.view.frame.size.width, )];
view.backgroundColor = [UIColor cyanColor];
//写"更多"的按钮
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
//写有省名的标签
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
[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:];
[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 = ; i < ; i++) {
NSString *string = self.proArr[i][@"proName"];
[array addObject:[string substringToIndex:]];
}
//第二种方法 : 对数组进行遍历然后放到数组中
// 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(, , WIDTH / , HEIGHT)];
self.proTableView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.proTableView];
[_proTableView release]; self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / , , WIDTH / , HEIGHT - )];
self.cityTableView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.cityTableView ];
[_cityTableView release]; self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / * , , WIDTH / , HEIGHT - )];
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

多个TableView的练习的更多相关文章

  1. iOS有关横向TableView的东西

    之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionvie ...

  2. tableView显示第一个cell有偏移问题

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...

  3. [tableView reloadData] 和 runloop

    需要[tableView reloadData]后需要立即获取tableview的cell.高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是会有问题的. 断点调试感觉 ...

  4. 【代码笔记】iOS-一个tableView,两个section

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  5. 【Swift】Alamofile网络请求数据更新TableView的坑

    写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...

  6. TableView 滑动收起键盘

    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 拖拽tableView就会收起键盘

  7. 关于TableView上有一段留白的解决方法

    当cell的类型是plaint类型时 直接设置self.automaticallyAdjustsScrollViewInsets=NO; 还有要注意检查你自己设置的frame是否正确     当cel ...

  8. iOS监听tableView组头切换事件

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSIntege ...

  9. 【原】iOS学习之tableView的常见BUG

    1.TableView头视图不随视图移动,头视图出现错位 错误原因:tableView的 UITableViewStyle 没有明确的声明 解决方法:在tableView声明的时候明确为 UITabl ...

  10. 两种让tableview返回顶部的方法

    1. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animat ...

随机推荐

  1. Swift 语法笔记01

    Swift 好多新奇的地方啊...妈的 var display: int { get() set() } Tuple: let x: (d:Double, e:String, f:Int) = (3. ...

  2. django模型

    用django时,只要用到数据库就得用到模型. 一.数据库的MTV开发模式 从MVC到MTV 所谓软件架构的MVC模式将数据的存取逻辑(Module),表现逻辑(View)和业务逻辑(Controll ...

  3. 机器学习:logistic回归

    逻辑回归是一个形式是Y=1/(1+E(-X))的函数,它的特点是: 1, 当X>0,随着X增大,Y很快的接近1: 2,当x<0,随着X的减小,Y很快的接近0: 3,当X=0时,Y=1/2. ...

  4. 深搜+剪枝 POJ 1724 ROADS

    POJ 1724 ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12766   Accepted: 4722 D ...

  5. Unity4.6新UI系统初探(uGUI)

    一.引言 Unity终于在即将到来的4.6版本内集成了所见即所得的UI解决方案(视频).事实上从近几个版本开始,Unity就在为这套系统做技术扩展,以保证最终能实现较理想的UI系统.本文试图通过初步的 ...

  6. QTP基础学习(二)启动与设置

    1.启动QTP选择要求的Add-in 默认带有3个Add-in,之后可以安装其他的Add-in,如.net的Add-in 2.设置QTP的选项 点击Tools-Options,弹出如下框: 3.建立记 ...

  7. Windows系统安装Oracle 11g客户端

    一.下载 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下网址来源此官方下载页网 ...

  8. Fidder--实现手机的抓包

    今天闲着没吊事,来写一篇关于怎么抓取Android中的app数据包?工欲行其事,必先利其器,上网google了一下,发现了一款神器:Fiddler,这个貌似是所有软件开发者必备神器呀!这款工具不仅可以 ...

  9. Sql注入截取字符串常用函数

    在sql注入中,往往会用到截取字符串的问题,例如不回显的情况下进行的注入,也成为盲注,这种情况下往往需要一个一个字符的去猜解,过程中需要用到截取字符串.本文中主要列举三个函数和该函数注入过程中的一些用 ...

  10. 27Spring_的事务管理_银行转账业务加上事务控制_基于tx.aop进行声明式事务管理

    上一篇文章中,银行转账业务没有使用事务,会出现问题,所以这篇文章对上篇文章出现的问题进行修改. 事务 依赖 AOP , AOP需要定义切面, 切面由Advice(通知) 和 PointCut(切点) ...