效果图:

左边图片的代码:

//
// 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. c# App.Config详解

    c# App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根 ...

  2. WEB安全--CSRF剖析

    CSRF攻击:攻击者构造合法的HTTP请求,随后利用用户的身份操作用户帐户的一种攻击方式. 一.CSRF攻击原理CSRF的攻击建立在浏览器与Web服务器的会话中:欺骗用户访问URL.二.CSRF攻击场 ...

  3. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  4. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

  5. 安装docker1.10

    1.安装 关闭 /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can ...

  6. bzoj-2338 2338: [HNOI2011]数矩形(计算几何)

    题目链接: 2338: [HNOI2011]数矩形 Time Limit: 20 Sec  Memory Limit: 128 MB Description Input   Output 题意: 思路 ...

  7. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  8. HDU 3333 Turing Tree --树状数组+离线处理

    题意:统计一段序列[L,R]的和,重复元素只算一次. 解法:容易看出在线做很难处理重复的情况,干脆全部讲查询读进来,然后将查询根据右端点排个序,然后离散化数据以后就可以操作了. 每次读入一个数,如果这 ...

  9. 2014 Super Training #6 F Search in the Wiki --集合取交+暴力

    原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...

  10. HDU 1085 Holding Bin-Laden Captive --生成函数第一题

    生成函数题. 题意:有币值1,2,5的硬币若干,问你最小的不能组成的币值为多少. 解法:写出生成函数: 然后求每项的系数即可. 因为三种硬币最多1000枚,1*1000+2*1000+5*1000=8 ...