UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
UITableViewStylePlain 不分区 表头标题可以固定到该分区的最后一行画出的时候改变
UITableViewStyleGrouped 分区 标题显示在
*/
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
// 设置重要属性
// 一个数据源 一个代理的属性
tableView.dataSource = self;
tableView.delegate = self;
// 设置表头和表尾
// 这个是整个tableView的表头和表尾
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 100)]; // 最好设置375
headView.backgroundColor = [UIColor greenColor];
tableView.tableHeaderView = headView;
[headView release];
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
footView.backgroundColor = [UIColor redColor];
tableView.tableFooterView = footView;
[footView release];
[self.view addSubview:tableView];
}
#pragma mark ---- datasource 两个必须实现的方法
// 返回每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 15;
}
{
// 创建一个UITableViewCell 并返回
// 四种样式 默认 reuseIdentifier 标识符
/*
// 详情标题
cell.detailTextLabel.text = @"df";
cell.textLabel.text = @"haha ";
UITableViewCellStyleDefault 子标题不显示
UITableViewCellStyleSubtitle 子标题在下面
UITableViewCellStyleValue1 子标题在右面
UITableViewCellStyleValue2 子标题压缩了 有图片不用这个
*/ UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"CKCell"];
cell.imageView.image = [UIImage imageNamed:@"faye1"];
cell.detailTextLabel.text = @"df";
cell.textLabel.text = @"haha ";
// cell 右面按钮的样式 (四种样式 勾 圈 箭头 圈和箭头)
cell.accessoryType = UITableViewCellAccessoryDetailButton;
return [cell autorelease];
// 不用 release ?????????????
}
NSLog(@"%ld", section);
// 返回 每个分区 表头的标题信息
// return @"A";
if (section == 0) {
return @"a";
} else if (section == 1) {
return @"b";
} else {
return @"c";
}
}
// 在tableView上 分区索引的标题们
return @[@"a",@"b",@"c"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
{
UIView *headView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 5)]autorelease]; // 最好设置375
headView.backgroundColor = [UIColor orangeColor];
return headView;
}
{
return 50;
}
{
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 5)];
footView.backgroundColor = [UIColor grayColor];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
* 1. 创建的时候有两种样式
2. 有两个代理 datasourse(两个方法必须执行必须实现的
->1. 分区有多少行,
->2. 每行都是什么样式的cell
<- 有几个分区
<- 表头表尾的标题
)
{
[_dataArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addTableView];
// Do any additional setup after loading the view.
{
self.dataArray = @[@"思聪",@"小芳",@"霞",@"莹",@"岩"];
}
- (void)addTableView
{
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
// 设置代理 数据源
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
{
[self setUpDate];
// NSLog(@"我是第%ld分区的", section);
// 返回每个分区有多少行
return self.dataArray.count;
{
#pragma mark ---以后禁止这么写
// NSIndexPath有行和分区的属性
// NSLog(@"我是%ld分区的%ld行", indexPath.section, indexPath.row);
// UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
// 利用 行数 indexpath.row 的变化 按照下标 动态的取出数据
// cell.textLabel.text = self.dataArray[indexPath.row];
// return cell;
#pragma mark ---重用机制
/**
* 重用机制
1. 首先要知道屏幕显示几条数据 比如 5条数据
2. 当向上滑的时候,有两个创建的选择 先要去集合里面找, 找不到就创建一个(第一次找的时候肯定没有,需要创建一个的)
3. 当第一个cell 完全划出屏幕的时候 这时候该cell 变成未显示状态,然后扔进集合里面.
4. 当下面再需要新出来的一个的时候,先去集合中找未显示状态的,有旧直接用,没有就去创建.
5. 如此循环 终成正果
~~~~~~~~可变集合中刚开始显示几个 集合中就有几个(只不过是显示状态的) 最多这个集合中有几个? 显示的数量 + 1 个.
*/
#pragma mark ---以后这么写
// 先写一个标识符 加上static 就只初始化一次
static NSString *identifier = @"JJCell";
// 去集合里面取找 有的话就使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier]autorelease];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
{
NSArray *groupD = [NSArray arrayWithObjects:
@"尚亚雷",
@"李永乐", nil];
NSArray *groupC = [NSArray arrayWithObjects:
@"李云飞",
@"莫亮萍", nil];
NSArray *groupM = [NSArray arrayWithObjects:
@"陈文博",
@"孙永飞",
@"王东旭", nil];
NSArray *groupN = [NSArray arrayWithObjects:
@"闫亚军",
@"仝兴伟",
@"代秋田",
@"何亚楠",
@"张宏宇",
@"朱海明",
@"杨松", nil];
NSArray *groupE = [NSArray arrayWithObjects:
@"于慧霞",
@"庞仕山",
@"陈岩岩",
@"柳中帅",
@"冯贵宾",
@"杨秘",
@"杨雪蕾",
@"王强",
@"薛石磊",
@"王增双",
@"裴小芳",
@"毛军军",nil];
NSArray *groupK= [NSArray arrayWithObjects:
@"张国文",
@"张莹莹",
@"郑明",
@"张莹莹",
@"崔锴",
@"蒲元凯",
@"顾成辉",
@"王向明",
@"冯振玲",
@"陈健健",
@"夏炎",
@"王昊",
@"武自健",nil];
NSArray *groupA = [NSArray arrayWithObjects:
@"卢奕霖",
@"王艺拓",
@"李雨",
@"刘新林",
@"田菲菲",
@"李琳冰",
@"王岩",
@"郭杰",
@"杨淋淇",
@"肖扬",
@"黎一良",
@"谷浩",
@"孙昊坤",nil];
NSArray *groupZ = [NSArray arrayWithObjects:
@"赵枫俊",
@"张明军",
@"刘迪",
@"聂银龙",
@"张博",
@"王珂", nil];
self.dataDic = @{@"A":groupA, @"Z":groupZ, @"C":groupC, @"D":groupD, @"K":groupK, @"E":groupE, @"M":groupM, @"N":groupN};
// 5. 取出所有的KEYs 进行排序
NSArray *keys = [self.dataDic allKeys];
self.sortKeysArray = [keys sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", self.sortKeysArray);
}
- (void)addTableView
{
// 1.
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
// 2.代理
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];
}
// 3. 多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
// 把对应的数组取出来
NSString *key = self.sortKeysArray[section];
NSArray *values = self.dataDic[key];
return values.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"JJCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier]autorelease];
}
// 6.
// 显示数据 利用indexPath.section 分区 取出key
NSString *key = self.sortKeysArray[indexPath.section];
// 利用 key 值来取出 每一个数组
NSArray *values = self.dataDic[key];
// 利用 indexPath.row 每个分区的行数 来 取出数组中的字符串
cell.textLabel.text = [values objectAtIndex:indexPath.row];
return cell;
}
// 4. 返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 有多少键值对就返回多少
return self.dataDic.count;
}
// 7. 现加表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// 用分区取出key
NSString *key = self.sortKeysArray[section];
return key;
}
// 8. 右面的点击的 contentOFFSet 偏移量
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.sortKeysArray;
- (void)setUpData
{
// 读取文件夹中的数据文件plist文件
// 工程的目录 [NSBundle mainBundle]
NSString *path = [[NSBundle mainBundle] pathForResource:@"TableViewPlist" ofType:@"plist"];
// 将文件中的数据拿出来 放到字典里面
NSDictionary *plistDic = [NSDictionary dictionaryWithContentsOfFile:path];
// 我从定义属性字典过来
// 循环外面初始化这个大字典
self.dataDic = [NSMutableDictionary dictionary];
// 取出所有的Key
NSArray *keys = [plistDic allKeys];
for (int i = 0; i < keys.count; i++) {
// 先把Key 取出来
NSString *key = keys[i];
// 取出每个key 对应的数组
NSArray *values =[plistDic objectForKey:key];
// 我从下面的KVC跳上来的
// 创建数组 保存赋值完成的model
NSMutableArray *tempArray = [NSMutableArray array];
// 继续遍历每个数组
for (NSDictionary *dic in values) {
// 一个字典对应一个model
// 创建model
CellModel *model = [[CellModel alloc]init];
// 对model 进行赋值
// 1. 一般赋值 一般不这么写 属性多了的话 太麻烦
//model.title = [dic objectForKey:@"title"];
//(KVC)赋值 一般都这么写
[model setValuesForKeysWithDictionary:dic];
// 把 model 加到临时的可变数组里
[tempArray addObject:model];
[model release];
}
[self.dataDic setObject:tempArray forKey:key];
}
NSLog(@"%@", self.dataDic);
}
- (void)addTableView
{
UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取出所有key
NSArray *keys = [self.dataDic allKeys];
NSString *key = keys[section];
NSArray *values = [self.dataDic objectForKey:key];
return values.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"JJcell";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
}
// 取出所有key
NSArray *keys = [self.dataDic allKeys];
NSString *key = keys[indexPath.section];
NSArray *values = [self.dataDic objectForKey:key];
CellModel *model = values[indexPath.row];
cell.textLabel.text = model.title;
cell.imageView.image =[UIImage imageNamed:model.imageName];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataDic.count;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"第%ld 分区 第%ld 行", indexPath.section, indexPath.row);
{
[_title release];
[_imageName release];
[super dealloc];
{
UI一揽子计划 9 (UITableView 、UITableView 、重用机制)相关推荐
- UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)
一.tableView的编辑 tableView 编辑的步骤: * 1. 让tableView成为可编辑状态 编辑按钮触发方法 * -1. 激活编辑状态 - (void)r ...
- UI一揽子计划 18 (沙盒机制、简单对象写入文件、NSFileMange、复杂对象写入文件)
1. 沙盒机制 数据持久化的原因及本质: 存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的.数据持久化是将数据保存成文件,存储到程序的沙盒中. 每个应用程序都有独立的沙盒,就是一个文 ...
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
Pro : SDWebImage第三方类库加载图片的使用: 1.在MRC 环境下 使用ARC 的类库: -fobjc-arc (Build Phases --> Compile Sourc ...
- UI一揽子计划 14 (拖控件 、)
- (void)viewDidLoad { [super viewDidLoad]; // 注册cell // 如果使用Xib 文件加载cell 的话 一定要注 ...
- UI一揽子计划 24 (MVC、通知、)
一.MVC Model - View - Controller 即 模型 - 视图 - 控制器 Modle (模型) 存储 处理数据 为应用程序提供数据. View (视图)展示用户界面视图,提供 ...
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...
- UI一揽子计划 1 (UIView UILabel)
第一天 1.UIView ·建立一个空模板后,首先将ARC模式关闭,因为iOS采用MRC模式,即手动管理内存. ·重写dealloc方法. - (void)dealloc { [_window rel ...
- UI一揽子计划 17 (image的异步加载、KVO观察者模式、KVO进行豆瓣列表界面图片的异步加载)
把下载图片的封装起来 ImageDownloader.h #import <Foundation/Foundation.h> @protocol ImageDownloaderDel ...
- UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
一.UINavigationController // 创建一个导航控制器 // 创建一个控制器作为根控制器 去管理 RootViewController *rootVC ...
最新文章
- 用c语言大一个三角形图形,c语言图形,请高手修改,效果是一个三角形绕一点旋转一周...
- 【深度学习入门到精通系列】拿捏Dice系数
- 类似QQ管家页面jquery图片显隐轮换效果
- DCMTK:OFStandard中“转换为标记”代码的测试程序
- 手把手教你玩转网络编程模型之完成例程(Completion Routine)
- Windows Phone 修改系统定义的资源颜色
- Windows Phone开发(46):与Socket有个约会 转:http://blog.csdn.net/tcjiaan/article/details/7669315...
- html编程入门指南,给萌新HTML5 入门指南
- Python学习之==线程进程
- java 可见_Java可见性机制的原理
- 成为软件高手的几个忌讳(转贴)
- 什么是隐形门? 隐形门安装要注意什么
- 深度学习自学(二十九):上下文感知和对规模不敏感的动作重复计数
- python、声明全局变量的关键字是_python基础之全局变量与global关键字
- mysql实际项目中使用多长时间_mysql常在项目中使用的语句总结
- mistake - pwnable
- 设置此计算机的用户已选择屏蔽此网站,已阻止此网站显示有安全证书错误的内容,详细教您已阻止此网站显示有安全证书错误的内容怎么解决...
- props传值强校验validator
- 软件工程之高质量代码(编码规范)
- 华为全连MGRE与星型拓扑MGRE(全网状与非全网状)