多个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(, , 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的练习的更多相关文章
- iOS有关横向TableView的东西
之前看到Apple store里面有横向的tableview,当然也有可能是collectionview啦. 尤其是项目中只有一条那么需要横向滑动的东西,就没有必要使用庞大的collectionvie ...
- tableView显示第一个cell有偏移问题
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...
- [tableView reloadData] 和 runloop
需要[tableView reloadData]后需要立即获取tableview的cell.高度,或者需要滚动tableview,那么,直接在reloadData后执行代码是会有问题的. 断点调试感觉 ...
- 【代码笔记】iOS-一个tableView,两个section
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 【Swift】Alamofile网络请求数据更新TableView的坑
写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...
- TableView 滑动收起键盘
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 拖拽tableView就会收起键盘
- 关于TableView上有一段留白的解决方法
当cell的类型是plaint类型时 直接设置self.automaticallyAdjustsScrollViewInsets=NO; 还有要注意检查你自己设置的frame是否正确 当cel ...
- iOS监听tableView组头切换事件
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSIntege ...
- 【原】iOS学习之tableView的常见BUG
1.TableView头视图不随视图移动,头视图出现错位 错误原因:tableView的 UITableViewStyle 没有明确的声明 解决方法:在tableView声明的时候明确为 UITabl ...
- 两种让tableview返回顶部的方法
1. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:_currentRow inSection:0] animat ...
随机推荐
- Mathout 安装部署
安装Mahout,并运行测试样例,抓图测试实验过程 证明已部署成功 Mahout 下载地址:http://apache.dataguru.cn/mahout/0.9/mahout-distributi ...
- Web性能优化之图片延迟加载
来源:微信公众号CodeL 对于一些图片多,页面长的网页来说,如果每次打开页面加载全部的网页内容,页面加载速度势必会受到影响,如果每次打开网页只将网页可视区域的内容加载给用户 ,将大大提高网页浏览速度 ...
- Windows路由表详解
对于路由器的路由表,大部分网管朋友都很熟悉,但是对于windows的路由表,可能了解的人就相对少一些.今天我们就一起来看看windows路由表. 一. windows路由表条目解释 1. 使用ip ...
- hdu-5495 LCS(置换)
题目链接: LCS Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- UVALive 6449 IQ Test --高斯消元?
题意:给你一串数字,问这串数字符合f[n] = a*f[n-1],f[n] = a*f[n-1]+b*f[n-2],f[n] = a*f[n-1]+b*f[n-2]+c*f[n-3]这几个方程中的哪个 ...
- Converting a Polygon ZM shape file to a regular Shape Polygon
from:http://blog.csdn.net/qb371/article/details/8102109 Locate the following tool - ArcToolbox > ...
- IOS定位服务的应用
IOS定位服务的应用 一.授权的申请与设置 二.定位服务相关方法 三.定位服务代理的相关方法 四.定位服务获取到的位置对象 五.航标定位得到的航标信息对象 IOS定位服务的应用 一.授权的申请与设置 ...
- Java中的IO流系统详解(转载)
摘要: Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java ...
- Android开发探秘之二:导入存在的项目及其注意事项
网上看到有jsoup写的例子,就下载下来进行了研究,但是发现不会导入,于是就百度一下,发现了方法:也就是依次点击“File”->“Import”->“General”->“Exist ...
- JS 之性能优化(2)
继续上一篇的JS性能优化之后,下面接着讲关于前端性能优化的内容.如果有不对的地方欢迎纠正. 1.避免过多的重排与重绘操作. 尽量将DOM中的多个读操作放一起,中间不要插入写的操作,因为写操作会导致浏览 ...