IOS第七天(3:UiTableView 模型和数据的分组的显示)
*************UiTableView模型和数据的分组的显示
#import "HMViewController.h"
#import "HMHero.h" @interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *heros;
@end @implementation HMViewController - (NSArray *)heros
{
if (_heros == nil) _heros = [HMHero heros];
return _heros;
} /**
UITableViewStylePlain, // 平板的格式
UITableViewStyleGrouped // 分组的格式
*/
- (UITableView *)tableView
{ if (_tableView == nil) {
// 表格控件在创建时必须指定样式,只能使用以下实例化方法
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self;
_tableView.delegate = self; [self.view addSubview:_tableView];
}
return _tableView;
} - (void)viewDidLoad
{
[super viewDidLoad]; [self tableView]; // 设置行高
self.tableView.rowHeight = ;
} #pragma mark - 数据源方法
// 每个分组中的数据总数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"每个分组的数据总数 %d", self.heros.count); return self.heros.count;
} /**
UITableViewCellStyleDefault, 默认类型 标题+可选图像
UITableViewCellStyleValue1, 标题+明细+图像
UITableViewCellStyleValue2, 不显示图像,标题+明细
UITableViewCellStyleSubtitle 标题+明细+图像
*/
// 告诉表格每个单元格的明细信息
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"表格行明细 %d", indexPath.row); UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // 取出英雄对象
HMHero *hero = self.heros[indexPath.row]; // 标题
cell.textLabel.text = hero.name;
// 明细信息
cell.detailTextLabel.text = hero.intro;
// 图像
cell.imageView.image = [UIImage imageNamed:hero.icon]; // 设置右边的箭头
// 1> UITableViewCellAccessoryDisclosureIndicator 箭头,可以"提示"用户,当前行是可以点击的,通常选中行,会跳到新的页面
// 2> UITableViewCellAccessoryCheckmark 对号,通常提示用户该行数据设置完毕,使用的比较少
// 3> UITableViewCellAccessoryDetailButton 按钮,通常点击按钮可以做独立的操作,例如alertView
// 点击按钮并不会选中行
// 4> UITableViewCellAccessoryDetailDisclosureButton 按钮+箭头,各自操作
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 指定右侧的自定义视图
/**
通常accessoryType提供的类型不能满足时,才会使用自定义控件 但是需要自行添加监听方法,通常用在自定义cell,不要写在视图控制器中!!! 自定义控件的事件触发,同样不会影响表格行的选中!
*/
UISwitch *switcher = [[UISwitch alloc] init];
// 添加监听方法
[switcher addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; cell.accessoryView = switcher; return cell;
} - (void)switchChanged:(UISwitch *)sender
{
NSLog(@"%s %@", __func__, sender);
} #pragma mark - 代理方法
// accessoryType为按钮时,点击右侧按钮的监听方法
// 此方法不会触发行选中,跟行选中各自独立
// 只是为accessoryType服务,对自定义控件不响应
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@", __func__, indexPath);
} // 取消选中某一行,极少用,极容易出错!
// didDeselectRowAtIndexPath
// didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@", __func__, indexPath);
} // 选中了某一行,有箭头的
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s %@", __func__, indexPath);
} /**
代理方法的优先级比rowHeight优先级高 应用场景:很多应用程序,每一行的高度是不一样的,例如:新浪微博 表格工作观察的小结 1> 要知道总共有多少数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 2> 计算“每一行”的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 问题:在此方法执行时,cell被实例化了吗?
方法的作用是什么? scrollView需要指定contentSize才能够滚动,如果没有实现方法,行高默认是44 需要知道每一行的高度,才能够准确的计算出contentSize 知道每一行的高度后,自然知道每一个屏幕应该显示多少行,表格明细方法的执行次数就知道了 3> 表格明细
调用屏幕显示所需要的行数,懒加载,只有要显示的表格行,才会被实例化! 小的结论: * tableView.rowHeight: 效率高,适用于所有的表格行高度一致
* 代理方法指定行高: 效率差,适合于每一个行的行高不一样,能够让表格更加的灵活 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(@"行高 %d", indexPath.row);
//
return (indexPath.row % ) ? : ; // 以下代码可以使用rowHeight属性替换!
// return 60;
} @end
IOS第七天(3:UiTableView 模型和数据的分组的显示)的更多相关文章
- IOS第七天(5:UiTableView 汽车品牌,复杂模型分组展示,A-Z索要列表) (2015-08-05 14:03)
复杂模型分组展示 #import "HMViewController.h" #import "HMCarGroup.h" #import "HMCar ...
- IOS第七天(6:UiTableView编辑模式, 拖动位置 ,滑动删除)
**********UiTableView编辑模式, 拖动位置 ,滑动删除 #import "HMViewController.h" @interface HMViewContro ...
- IOS第七天(2:UiTableView 加上数据分离)
****加上数据分离 #import "HMViewController.h" #import "HMStudent.h" @interface HMViewC ...
- IOS第七天(1:UiTableView 的基本用法)
***表格控件 #import "HMViewController.h" @interface HMViewController () <UITableViewDataSou ...
- IOS第七天(4:UiTableView 数据的显示优化重复实例和tableFooterView和tableHeaderView)
//加上头部 和底部 - (void)viewDidLoad { [super viewDidLoad]; [self tableView]; // 设置行高 self.tableView.rowHe ...
- iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序
iOS开发UI篇—使用嵌套模型完成的一个简单汽车图标展示程序 一.plist文件和项目结构图 说明:这是一个嵌套模型的示例 二.代码示例: YYcarsgroup.h文件代码: // // YYcar ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
随机推荐
- 本BLOG简介(内有一道UVa524素数环进阶版)【B001】
[B001]Hi,大家好,今天我的博客第一天开通,今天奉上开博题,出自首都师师范大学附属中学OJ(题号未知在练习场中)原题为UVa524,题目要求如下: [难度B]—————————————————— ...
- Monkeyrunner脚本中component快速定位方法
在编写MonkeyRunner脚本过程中,会出现component这一项内容,很多人可能不知道怎么确认,其实这个主要是为了指定要测试的程序包名和主Activity名,我们可以用以下方法去进行确认: 1 ...
- Bower In ASP.NET Core
创建一个ASP.NET Core MVC项目的时候,会产生一个bower.json的文件,用于管理前段的js. NPM & Bower NPM主要运用于Node.js项目的内部依赖包管理,安装 ...
- MongoDB 入门之基础 DDL
此文章主要记录部分主要的 MongoDB 的 DDL 操作. db 查看当前所在的数据库(默认 test) > db test > show dbs 查看当前数据库服务器上的数据库名字 ...
- js事件绑定细节说明
javascript绑定事件: 经常用jQuery去写,时间长了对原生态的js事件绑定的知识会慢慢淡化或者遗忘了,必须翻出来再次总结,今天再次把js原生态事件的处理做个总结. 从最初开始,谁刚接触ja ...
- 基于单决策树的AdaBoost
①起源:Boosting算法 Boosting算法的目的是每次基于全部数据集,通过使用同一种分类器不同的抽取参数方法(如决策树,每次都可以抽取不同的特征维度来剖分数据集) 训练一些不同弱分类器(单次分 ...
- NOIp 2013 #3 转圈游戏 Label:模拟
题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此 ...
- 摘:JavaScript性能优化小知识总结
原文地址:http://www.codeceo.com/article/javascript-performance-tips.html JavaScript的性能问题不容小觑,这就需要我们开发人员在 ...
- Android --ToggleButton的使用
1. 效果图
- 你也可以用java的swing可以做出这么炫的mp3播放器_源码下载
I had published the blog : 你用java的swing可以做出这么炫的mp3播放器吗? and to display some screenshots about this M ...