多个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 ...
随机推荐
- C++STL之迭代器2
在学习c++ STL的时候,整天碰到迭代器,也整天用,但是,到底它是个什么东西,很多人没有一个认识.这里我通过几个小的DEMO,来看看迭代器.首先我实现了一个十分简陋的vector类: templat ...
- 迅为4412开发板Linux驱动教程之GPIO的初始化
视频下载地址:http://pan.baidu.com/s/1c06oiti GPIO的初始化 • 在内核源码目录下使用命令“ls drivers/gpio/*.o”,可以看到“gpio-exynos ...
- Java语法结构
一.顺序结构(从上往下依次执行) 顺序结构语法比较简单,从上往下依次执行即可. 二.选择结构(选择性执行,如果....则.....) 1.if 语句 if语句,作用是根据判断结果为真或假,选择其中一个 ...
- 详解Webwork中Action 调用的方法
详解Webwork中Action 调用的方法 从三方面介绍webwork action调用相关知识: 1.Webwork 获取和包装 web 参数 2.这部分框架类关系 3.DefaultAction ...
- java 20 -2 递归之找特定目录下的特定格式文件
/* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...
- java 19 -14 File类的判断并输出案例
package zl_file; import java.io.File; import java.io.FilenameFilter; /* 需求: 判断E盘目录下是否有后缀名为.jpg的文件,如果 ...
- SVN和git的使用(附github的简单玩法)
今天简单的总结了下SVN和git的使用,也尝试了下github,应该好好提高下自己的英文水平了,梦想有一天不再使用任何翻译软件. [svn]:集中式的代码管理工具(版本控制工具--版本记录) 1> ...
- 介绍Git版本控制器的使用
Git 简介 Git 是什么?大家肯定会说不就是版本控制器嘛,是的Git是目前世界上最先进的分布式版本控制系统(没有之一). 1.那什么是版本控制器呢? 举个简单的例子,比如我们用Word写文章,那你 ...
- javascript中的hasOwnProperty和isPrototypeOf
hasOwnProperty:是用来判断一个对象是否有你给出名称的属性或对象.不过需要注意的是,此方法无法检查该对象的原型链中是否具有该属性,该属性必须是对象本身的一个成员.isPrototypeOf ...
- prepareStatement createStatement
preparedstatement具备很多优点,开发者可能通常都使用它,只有在完全是因为性能原因或者是在一行sql语句中没有变量的时候才使用通常的statement. preparedstatemen ...