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 ...
随机推荐
- Dinic算法学习&&HDU2063
http://www.cnblogs.com/SYCstudio/p/7260613.html 看这篇博文懂了一点,做题再体会体会吧 找了好久都没找到一个好用的模板…… 我也是佛了..最后决定用峰神的 ...
- 强联通分量(tarjan算法+算法简介)
题目描述 对于一个有向图顶点的子集S,如果在S内任取两个顶点u和v,都能找到一条从u到v的路径,那么就称S是强连通的.如果在强连通的顶点集合S中加入其他任意顶点集合后,它都不再是强连通的,那么就称S ...
- c++ 操作防火墙
原文转自 https://msdn.microsoft.com/zh-cn/library/aa364726(v=vs.85).aspx #include <windows.h> #inc ...
- c中结构体的4种定义
1.常规的标准方式: 1 #include <stdio.h> 2 3 struct student{ 4 int age; 5 float score; 6 ...
- c++ 实现 http 上传和下载
代码下载地址: http://download.csdn.net/detail/mtour/8243527 最近写了个程序需要用到http通讯,由于flash空间比较小,没有考虑 libcurl库 ...
- hdu 5067(暴力搜索)
Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- (1)WPF概述
一.wpf特点 winfrom使用GID/GID+ 图形引擎,wpf使用directx的图形引擎 通过directx提供硬件加速 类型web的布局模型 丰富的绘图模型.文本模型, 支持音频视频 可创建 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] A. Raising Bacteria【位运算/二进制拆分/细胞繁殖,每天倍增】
A. Raising Bacteria time limit per test 1 second memory limit per test 256 megabytes input standard ...
- java标识符与命名规则
标识符就是给变量.类或方法起的名字.可以用字母.下划线或美元符号开头,区分大小写,没有最大长度限制.(关键字除外) 关键字 访问控制 private protected public ...
- 分享Kali Linux 2017年第24周镜像文件
分享Kali Linux 2017年第24周镜像文件 Kali Linux官方于6月11日发布2017年的第24周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KD ...