UI_UITableView_搭建
创建 tableView
UITableViewStyle 有两种选择
#pragma mark - 创建 tableView
- (void)createTableView
{
// 枚举类型共同拥有两个
self.mainTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
[self addSubview:self.mainTableView];
}
根视图控制器遵守协议
@interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源代理
self.rootView.mainTableView.dataSource = self;
// 设置 delegate
self.rootView.mainTableView.delegate = self;
self.title = @"联系人";
}
重写代理方法
返回分组的个数
#pragma -mark 返回分组的个数
// 默觉得 1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
每一个分组显示多少行数据 必须
#pragma mark - 每一个分组显示多少行数据 *必须*
// dataSource 下方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.nameArray.count;
}
每行显示什么内容 必须
#pragma mark - 每行显示什么内容 *必须*
// dataSource 下方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 四种类型
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// static 作用
/**
* 1、清零功能 static int a;
* 2、保值作用
* 3、隐藏功能
*/
// 静态变量 保值作用 仅仅创建一次
static NSString *cell_id = @"UITableViewCell";
// 利用重用创建
UITableViewCell *cell = nil;
// 在重用池查找
cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
}
cell.textLabel.text = self.nameArray[indexPath.row];
// indexPath.section 那个分组
// indexPath.row 哪一行
NSString *name = [NSString stringWithFormat:@"%ld.png", indexPath.row + 1];
cell.imageView.image = [UIImage imageNamed:name];
// 显示具体信息 cell 类型用 UITableViewCellStyleSubtitle
cell.detailTextLabel.text = self.numberArray[indexPath.row];
// 右側附件button 枚举类型
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
row 的高度
#pragma mark - 跳转 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80.0f;
}
返回分组的名字
#pragma mark - 返回分组的名字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"分组一";
}
return @"分组二";
}
设置分组头部
#pragma mark - 设置分组头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
if (section == 0) {
label.text = @"1组";
} else {
label.text = @"2组";
}
return label;
}
设置分组头部的高度
#pragma mark - 设置分组头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60.0f;
}#pragma mark - 设置分组头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60.0f;
}
cell 的点击事件
#pragma mark - cell 的点击事件
- (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消选中状态
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}
实现索引
#pragma mark - 实现索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"1组", @"2组"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UI_UITableView_搭建的更多相关文章
- Online Judge(OJ)搭建(第一版)
搭建 OJ 需要的知识(重要性排序): Java SE(Basic Knowledge, String, FileWriter, JavaCompiler, URLClassLoader, Secur ...
- Angular2入门系列教程1-使用Angular-cli搭建Angular2开发环境
一直在学Angular2,百忙之中抽点时间来写个简单的教程. 2016年是前端飞速发展的一年,前端越来越形成了(web component)组件化的编程模式:以前Jquery通吃一切的田园时代一去不复 ...
- 总结:Mac前端开发环境的搭建(配置)
新年新气象,在2016年的第一天,我入手了人生中第一台自己的电脑(大一时好友赠送的电脑在一次无意中烧坏了主板,此后便不断借用别人的或者网站的).macbook air,身上已无分文...接下来半年的房 ...
- Angular企业级开发(5)-项目框架搭建
1.AngularJS Seed项目目录结构 AngularJS官方网站提供了一个angular-phonecat项目,另外一个就是Angular-Seed项目.所以大多数团队会基于Angular-S ...
- 【分享】标准springMVC+mybatis项目maven搭建最精简教程
文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...
- 一起学微软Power BI系列-使用技巧(4)Power BI中国版企业环境搭建和帐号问题
千呼万唤的Power BI中国版终于落地了,相信12月初的微软技术大会之后已经铺天盖地的新闻出现了,不错,Power BI中国版真的来了,但还有些遗憾,国际版的一些重量级服务如power bi emb ...
- 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...
- 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
随机推荐
- angular控制器controller里获取不到ng-model的值,获取为undefined
所遇问题: html:ng-model=“test”, 但是在controller里打印的$scope属性里面并未发现test,控制台打印test为undefined,页面上{{test}}却可以正常 ...
- s19文件格式详解
1.概述 为了在不同的计算机平台之间传输程序代码和数据,摩托罗拉将程序和数据文件以一种可打印的格式(ASCII格式)编码成s格式文件.s格式文件是Freescale推荐使用的标准文件传送格式.编译完成 ...
- Topcoder SRM 606 div1题解
打卡! Easy(250pts): 题目大意:一个人心中想了一个数,另一个人进行了n次猜测,每一次第一个人都会告诉他实际的数和猜测的数的差的绝对值是多少,现在告诉你所有的猜测和所有的差,要求你判断心中 ...
- [ CodeVS冲杯之路 ] P3145
不充钱,你怎么AC? 题目:http://codevs.cn/problem/3145/ 经典的汉诺塔问题 我们移动的时候,如果是最小的1号就可以直接移动,否则先将上面的x-1号先移动到借用塔上,然 ...
- ScrollLayer
http://www.oschina.net/p/scrolllayer ScrollLayer 编辑/纠错 分享到: 已用 +1 收藏 +12 4月18日 武汉 源创会开始报名,送华为开 ...
- UVA 10334 Ray Through Glasses
自己手动画了第三项发现f[3]=5;就猜斐波那契了.实际上光线分为两种距离外界有2面玻璃,1面玻璃 其分别时n-1次反射,n-2次反射形成的 故推出斐波那契. 手动一些f1,f2,f3就OK #inc ...
- [转]Google 的开源技术protobuf 简介与例子
本文来自CSDN博客:http://blog.csdn.NET/program_think/archive/2009/05/31/4229773.aspx 今天来介绍一下“Protocol Buffe ...
- POCO库中文编程参考指南(3)Poco::Net::Socket
POCO库中文编程参考指南(3)Poco::Net::Socket 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.c ...
- vagrant virtualbox VM inaccessible解决办法
vagrant无法访问的提示:Please open VirtualBox and clear out your inaccessible virtual machines or find a way ...
- [PHP] print_r()函数美化
function beautiful_print_r($arr){ echo '<pre>'; print_r($arr); echo '</pre>'; }